85

Possible Duplicate:
XML Table layout? Two EQUAL-width rows filled with equally width buttons?

I am using TableLayout to show list of data in 4 columns.

Problem description:

I am unable to set equal width of all 4 columns, which are in my TableLayout. I am putting my layout code, which I am using...

<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">
        <TableRow>
            <TextView android:text="table header1" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                 android:textSize="10dip" android:textStyle="bold"/>    
            <TextView android:text="table header2" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:textSize="10dip" android:textStyle="bold"/> 
            <TextView android:text="table header3" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:textSize="10dip" android:textStyle="bold"/> 
            <TextView android:text="table header4" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:textSize="10dip" android:textStyle="bold"/>         
        </TableRow>
    </TableLayout>

How should I rewrite this layout to show 4 columns with equal sizes?

1

2 Answers 2

221

Try this.

It boils down to adding android:stretchColumns="*" to your TableLayout root and setting android:layout_width="0dp" to all the children in your TableRows.

<TableLayout
    android:stretchColumns="*"   // Optionally use numbered list "0,1,2,3,..."
>
    <TableRow
        android:layout_width="0dp"
    >
4
  • 24
    Just a little note... I also had to add android:layout_weight="1" to the children of TableRows for it to work.
    – Asaf
    Commented Dec 2, 2013 at 10:13
  • 33
    stretchColumns should be "*", not 1 to have all columns be of equal width.
    – Yevgeniy
    Commented May 2, 2014 at 18:41
  • 6
    This solution works for me, but I was looking for at way to do it programmatically. I used setStretchAllColumns(true) on TableLayout and setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT)) on each TableRow child. Setting weight didn't seem to be necessary. Commented Dec 30, 2016 at 9:50
  • @Sherif elKhatib your solution works but when we set each column width by wrap_content. If the column data increased the column width will be increased as well and the columns will not be from the same size at this point. Do you have solution for this ? Can we set max_width for each column to be the equal desired width ? Commented Aug 13, 2017 at 8:48
77

Change android:stretchColumns value to *.

Value 0 means stretch the first column. Value 1 means stretch the second column and so on.

Value * means stretch all the columns.

5
  • 14
    Its not working, if column1 is text is more, its expanding.
    – Palani
    Commented Dec 8, 2011 at 8:08
  • 3
    Yep, this is not working
    – Ted
    Commented Feb 11, 2013 at 19:45
  • 14
    Just remove your android:stretchColumns="0" and add android:layout_weight="1" to all four and you get the desired output. Commented Jul 4, 2014 at 17:28
  • 8
    Don't forget to add android:layout_width="0dp", or weights won't be taken into account. Commented Aug 25, 2014 at 14:40
  • thanks its working like charm Commented Jul 9, 2019 at 10:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.