TableLayout

Android TableLayout going to be arranged groups of views into rows and columns. You will use the <TableRow> element to build a row in the table. Each row has zero or more cells; each cell can hold one View object.

1. TableLayoutAndroidExample.java

package com.tutorialsee.tablelayout;

import com.tutorialsee.tablelayout.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;

public class TableLayoutAndroidExample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_table_layout_android_example);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_table_layout_android_example,
menu);
return true;
}
}

2. activity_table_layout_android_example.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B2DFDB"
    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="#009688"
            android:padding="18dip"
            android:gravity="center"
            android:text="Row 1"
            android:textColor="#000"
            android:textSize="18dp" />
    </TableRow>

    <!-- Row 2 with 3 columns -->

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

        <TextView
            android:id="@+id/TextView04"
            android:layout_weight="1"
            android:background="#B2DFDB"
            android:gravity="center"
            android:padding="20dip"
            android:text="Row 2 column 1"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/TextView05"
            android:layout_weight="1"
            android:background="#FAFAFA"
            android:gravity="center"
            android:padding="20dip"
            android:layout_marginLeft="0.1dp"
            android:text="Row 2 column 2"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/TextView06"
            android:layout_weight="1"
            android:background="#B2DFDB"
            android:gravity="center"
            android:padding="20dip"
            android:text="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:id="@+id/TextView07"
            android:layout_weight="1"
            android:background="#3b7bea"
            android:gravity="center"
            android:padding="18dip"
            android:text="Row 3 column 1"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/TextView08"
            android:layout_weight="1"
            android:background="#f7991c"
            android:gravity="center"
            android:padding="18dip"
            android:text="Row 3 column 2"
            android:textColor="#000000" />
    </TableRow>

</TableLayout>