I'm trying to dynamically create TableRow
objects and add them to a TableLayout
.
The TableRow
objects has 2 items, a TextView
and a CheckBox
. The TextView
items need to have their layout weight set to 1 to push the CheckBox
items to the far right.
I can't find documentation on how to programmatically set the layout weight of a TextView
item.
just set layout params in that layout like
create param variable
1f is weight variable
set your widget or layout like
1f is denotes as weight=1; you can give 2f or 3f, views will move accoding to the space
The answer is that you have to use TableRow.LayoutParams, not LinearLayout.LayoutParams or any other LayoutParams.
The different LayoutParams are not interchangeable and if you use the wrong one then nothing seems to happen. The text view's parent is a table row, hence:
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
There is another way to do this. In case you need to set only one parameter, for example 'height':
In the earlier answers weight is passed to the constructor of a new SomeLayoutType.LayoutParams object. Still in many cases it's more convenient to use existing objects - it helps to avoid dealing with parameters we are not interested in.
An example:
This should works to you