Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of "colspan" in an Android TableLayout?

It seems that there is an attribute doing that : layout_span

UPDATE: This attribute must be applied to the children of the TableRow. NOT to the TableRow itself.


Just to complete the answer, the layout_span attribute must be added to the child, not to TableRow.

This snippet shows the third row of my tableLayout, which spans for 2 columns.

<TableLayout>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="@string/create" />
    </TableRow>
</TableLayout>

And this is how you do it programmatically

//theChild in this case is the child of TableRow
TableRow.LayoutParams params = (TableRow.LayoutParams) theChild.getLayoutParams();
params.span = 2; //amount of columns you will span
theChild.setLayoutParams(params);

You have to use layout_weight to fill the entire row otherwise it still fills left or right column of table layout.

<TableRow
  android:id="@+id/tableRow1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:layout_weight="1"
            android:text="ClickMe" />

    </TableRow>

Maybe this will help someone. I tried the solution with layout_span but this not working for me. So I solved the problem with this trick. Just use LinearLayout in place of TableRow where you need colspan, that's all.