Table layout adding image and textview into a row side by side pragmatically

2.1k views Asked by At

I'm trying to do something simple but it's giving me so much heartache. I'm trying to pragmatically create a table layout, add a row, insert an image and a text view right next to each other. The image should take 25% of the row the rest should be the TextView. I'm not stuck with TableLayout. If you think something is better, I'm all ears but please it has to be dynamic/pragamatically possible to do. Thanks in advance of any help fellows!

Here's what I tried. When I run it, nothing shows. I have a LinearLayout (ll) that I'm trying to add the table to:

                   TableLayout tl = new TableLayout(this);
                    TableRow tr = new TableRow(this);


                    //add user avatar image
                ImageView   uivd = new ImageView (this);
                    param = new LinearLayout.LayoutParams (175,
                        175);

                    uivd.Id = idi++;
                    uivd.SetMaxWidth(175);
                    uivd.SetMaxHeight(175);
                    uivd.SetPadding (10, 10, 10, 10);
                    var vimageBitmap = GetImageBitmapFromUrl    (objModel.BaseURL + item.AuthorAvatar);
                    uivd.SetImageBitmap (vimageBitmap);

                    param.SetMargins (10, 10, 10, 10);

                    //add the image to the table row
                    tr.AddView(uivd,0,param);




                    //add the textview should take 75% of the width
                    TextView v = new TextView (this);
                    v.Id = idi++;
                    param = new LinearLayout.LayoutParams   (ViewGroup.LayoutParams.WrapContent,
                        ViewGroup.LayoutParams.WrapContent);

                    param.SetMargins (15, 15, 15, 15);
                    v.SetBackgroundColor (Color.White);
                    v.SetTextColor (Color.Black);
                    v.SetTextSize (Android.Util.ComplexUnitType.Pt, 10);
                    v.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.textrounded));
                    v.Text = item.Title +
                    System.Environment.NewLine +item.Msg;

                    //add the textview to the table row
                    tr.AddView (v, 1, param);

                    //add the table row to the table
                    tl.AddView (tr);

                    //add the table to the Linearlayout at the index idx
                    ll.AddView (tl, idx++, param);
2

There are 2 answers

0
capt.swag On

I suppose using LinearLayout with weightSum attribute. The corresponding xml for this is. You could change this to Java. Although the concept uses the same.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"/>

</LinearLayout>

Here the weightSum of LinearLayout is 1. And the layout_weight of ImageView is 0.25. So it will be taking up 25% of the space. And the remaining 75% will be taken by the TextView.

0
Chandra Sharma On

Why you do not using Linear Layout or Relative Layout, it would be better for your requirement.check the below sample with Linear Layout.

        LinearLayout parentlayout = (LinearLayout)findViewById(R.id.ll);
        LinearLayout childLayout = new LinearLayout(this);

        childLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        childLayout.setOrientation(LinearLayout.HORIZONTAL);
        TextView tv = new TextView(this);
        tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.75f));
        tv.setTextColor(Color.BLACK);
        ImageView iv = new ImageView(this);
        iv.setBackgroundResource(R.drawable.icon);
        iv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.25f));
        tv.setText("This is the Text ");    
        tv.setTextSize(18.0f);
        childLayout.addView(iv);
        childLayout.addView(tv);
        parentlayout.addView(childLayout);