46

Possible Duplicate:
What is the equivalent of “colspan” in an Android TableLayout?

It says in the documentation for TableLayout "Cells can span columns, as they can in HTML." However, I can't find any way to do so.

Specifically, I have one row with two columns and another with one column. I want the one column row to span the entire table. Seems easy, but I don't see it.

Community
  • 1
  • 1
Shawn Lauzon
  • 6,234
  • 4
  • 35
  • 46

2 Answers2

80

add android:layout_span="3" to span 3 columns. For example:

        <TableRow>
            <Button android:id="@+id/item_id"
                android:layout_span="2"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="item text" />     
        </TableRow>
IncrediApp
  • 10,303
  • 2
  • 33
  • 24
  • 3
    Ahh, there it is, in TableRow.LayoutParams: http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html – Shawn Lauzon Aug 27 '11 at 06:37
  • 2
    Finding those layout XML files is tricky -- easy to forget the android:layout_* attributes are in separate javadocs. – Shawn Lauzon Aug 27 '11 at 18:28
  • 1
    -1, This solution does not work when the text of the button is too small. For example when the text is only a `+` like in a calculator app, the button will not grow until it covers two columns (if those two columns are wider than the button is without the columnSpan). – Zelphir Kaltstahl Jan 27 '16 at 12:46
  • 1
    Why we can't use weight on above view i.e button – eC Droid Jul 31 '17 at 14:11
3

android:layout_span does the trick.

example:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
    <TextView android:id="@+id/info"
        android:layout_span="2"
        android:text="some text"
        android:padding="3dip"
        android:layout_gravity="center_horizontal" />
</TableRow>
<TableRow>
    <TextView android:text="some other text" 
        android:padding="3dip" />
    <TextView android:text="more text" 
        android:padding="3dip" />
</TableRow>
</TableLayout>