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);
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.
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.