Android Stretch columns evenly in a TableLayout

Android

Android 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrahcirView Question on Stackoverflow
Solution 1 - AndroidSimonView Answer on Stackoverflow
Solution 2 - AndroidlinitbuffView Answer on Stackoverflow
Solution 3 - AndroidMeriamView Answer on Stackoverflow
Solution 4 - AndroidCommonsWareView Answer on Stackoverflow
Solution 5 - AndroidFDIMView Answer on Stackoverflow