Android Stretch columns evenly in a TableLayout
AndroidAndroid Problem Overview
I am displaying a table of values in my android application, and would like the columns to be distributed evenly in terms of size , instead of sizing according to content.
Been playing around with stretchColumns but couldn't manage to figure out the right combination, Any Ideas?
Android Solutions
Solution 1 - Android
I had the same problem - I always only entered one number in android:stretchColumns
, but you have to enter all columns that should be stretched. So if you have three columns, you have to write:
android:stretchColumns="0,1,2"
Or write:
android:stretchColumns="*"
Then all columns will have the same size. As a reminder, android:stretchColumns
is an attribute for the TableLayout
element.
Solution 2 - Android
You need to set BOTH android:layout_width="0dip"
and android:layout_weight="1"
for each view within a table row. I think this works because the weights determine the proportion of the EMPTY SPACE in the row used by the respective views, and since the width of all views is set to 0dip
, the whole row is empty space, and hence with equal weights the views are all allocated the same proportion of the WHOLE width.
Solution 3 - Android
Use the android:layout_weight
for the columns
Solution 4 - Android
You control the size of the columns by the size of their contents. If you want them all to be the same size, set that up via android:layout_width
in the appropriate cells.
Solution 5 - Android
Whoever is still looking for solution, for me: setting columns both shrinkable and stretchable did the trick. I had pretty much the same problem as OP.