0

I have a TableLayout and a row with a couple TextViews. I want to find the layout_weight of the TextView but I cant figure out how. I've tried the following:

TableRow.LayoutParams lp = tv1.getLayoutParams();

and:

ViewGroup.LayoutParams lp = tv1.getLayoutParams();

In the first case I get a type mismatch: "Cannot convert from ViewGroup.LayoutParams to TableRow.LayoutParams", and the second case works fine, however ViewGroup.LayoutParams doesn't have a weight field.

All the different types of LayoutParams are confusing, I'm never sure which to use where.

Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
linitbuff
  • 339
  • 3
  • 7
  • 1
    try this http://stackoverflow.com/questions/6689896/setting-the-layout-weight-of-the-textview-under-the-tablerow – Dixit Patel Jan 15 '13 at 09:32

2 Answers2

0

Try this

TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

Try this link

swiftBoy
  • 34,827
  • 26
  • 133
  • 130
Janmejoy
  • 2,723
  • 1
  • 17
  • 38
  • 3
    Setting the `LayoutParams` isn't a problem. It works with both `TableRow` and `TableLayout` `LayoutParams`. The problem is I can't get the `layout_weight` because `getLayoutParams()` returns a `ViewGroup.LayoutParams` object which doesn't include weight. – linitbuff Jan 16 '13 at 06:34
0

You almost had it with your first try. You just need to cast to the correct type:

TableRow.LayoutParams lp = (TableRow.LayoutParams) tv1.getLayoutParams();

Any subclass of LinearLayout.LayoutParams (including TableRow.LayoutParams) will have a weight field.

Ted Hopp
  • 227,407
  • 48
  • 383
  • 507