How to set number of columns in android tablelayout

Scarlett  picture Scarlett · May 15, 2016 · Viewed 13.9k times · Source

I have the following code:

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android: {code is required here}="2">

<TableRow>
    <TextView
        android:id="@+id/accelerometer_label"
        android: {code is required here}="1"
        android:text="Accelerometer"
        android:textSize="9pt"
        android:padding="3dip" />
</TableRow>

I am trying to get the data to display in three columns, any idea what code needs to go in the {code is required here} section?

Answer

Mrugesh Thaker picture Mrugesh Thaker · May 15, 2016
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:shrinkColumns="*"
    android:stretchColumns="*">

    <!-- Row 1 with single column -->
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:background="#b0b0b0"
            android:padding="18dip"
            android:text="@string/row_1"
            android:textColor="#000"
            android:textSize="18sp" />


    </TableRow>

    <!-- Row 2 with 3 columns -->
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_weight="1"
            android:background="#dcdcdc"
            android:gravity="center"
            android:padding="20dip"
            android:text="@string/row_2_column_1"
            android:textColor="#000000" />

        <TextView
            android:layout_weight="1"
            android:background="#d3d3d3"
            android:gravity="center"
            android:padding="20dip"
            android:text="@string/row_2_column_2"
            android:textColor="#000000" />

        <TextView
            android:layout_weight="1"
            android:background="#cac9c9"
            android:gravity="center"
            android:padding="20dip"
            android:text="@string/row_2_column_3"
            android:textColor="#000000" />

    </TableRow>

    <!-- Row 3 with 2 columns -->
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_weight="1"
            android:background="#b0b0b0"
            android:gravity="center"
            android:padding="18dip"
            android:text="@string/row_3_column_1"
            android:textColor="#000000" />

        <TextView
            android:layout_weight="1"
            android:background="#a09f9f"
            android:gravity="center"
            android:padding="18dip"
            android:text="@string/row_3_column_2"
            android:textColor="#000000" />
    </TableRow>

 </TableLayout>