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.
You can also give weight separately like this ,
(OR)
1f is refered as weight=1; according to your need you can give 2f or 3f, views will move accoding to the space. For making specified distance between views in Linear layout use weightsum for "LinearLayout".
I had a fair amount of difficulty with a solution something very similar to this: trying to have two buttons in a TableRow, with each being half the screen width. For whatever reason, the left button would always be about 70% of the width, and the right button 30%. Calling table_layout.setStretchAllColumns(true) had no effect, nor did setting the button's width to half the screen, nor setting their layout weight.
The solution I ended up with was nesting a LinearLayout in the TableRows, which did take into account the value of the buttons' width.
You have to use
TableLayout.LayoutParams
with something like this:The last parameter is the weight.
This work for me, and I hope it will work for you also
Set the LayoutParams for the parent view first:
then set for the TextView (child):