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
  • 50,140
  • 28
  • 121
  • 140
linitbuff
  • 359
  • 3
  • 8
  • 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
  • 35,607
  • 26
  • 136
  • 135
Janmejoy
  • 2,721
  • 1
  • 20
  • 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
  • 232,168
  • 48
  • 399
  • 521