TableLayout For Android

TableLayout is a viewGroup that displays child view elements in rows and columns. Table layout arranges its childern into rows and columns.Table Layout containers do not display a border line for their columns, rows or cells. A Table will have as many columns as the row with the most cells.

A table can leave cells empty. Cells can span multiple columns, as they can in HTML. You can span columns by using the span field in the TableRow.LayoutParams class.

Please note that Cells cannot span multiple rows.

The TableLayout in android will work same as HTML table and table will have as many columns as the row with the most cells. The TableLayout can be explained as <table> and TableRow is like <tr> element.



Please note that

TableLayout is just a layout manager, somewhat like a table in HTML. It does not itself do any scrolling, to have something that scrolls you must put the TableLayout in a ScrollView. This implies that all of the data you are displaying must be populated into the TableLayout up-front, so the ScrollView knows the total space it is to scroll in.
Table layout is useful if someone have low amount of data to display because every row of table layout will have to be instantiated and it won’t be recycled.

Important  attributes of the TableLayout

stretchColumns: Stretch column attribute is used in Table Layout to change the default width of a column which is set equal to the width of the widest column but we can also stretch the columns to take up available free space by using this attribute. The value that assigned to this attribute can be a single column number or a comma delimited list of column numbers (1, 2, 3…n).
If the value is 1 then the second column is stretched to take up any available space in the row, because of the column numbers are started from 0.
If the value is 0,1 then both the first and second columns of table are stretched to take up the available space in the row.
If the value is ‘*’ then all the columns are stretched to take up the available space.
Below is the example code of stretch column attribute of table layout with explanation included in which we stretch the first column of layout.

<?xml version="1.0" encoding="utf-8"?>

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/simpleTableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="1"> <!-- stretch the second column of the layout-->

        <!-- first row of the table layout-->
        <TableRow

            android:id="@+id/firstRow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"> 
 
 
shrinkColumns: Shrink column attribute is used to shrink or reduce the width of the column‘s. We can specify either a single column or a comma delimited list of column numbers for this attribute. The content in the specified columns word-wraps to reduce their width.
If the value is 0 then the first column’s width shrinks or reduces by word wrapping its content.
If the value is 0,1 then both first and second columns are shrinks or reduced by word wrapping its content.
If the value is ‘*’ then the content of all columns is word wrapped to shrink their widths.
Below is the example code of shrink column attribute of table layout with explanation included in which we shrink the first column of layout.

  <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="0"> <!-- shrink the first column of the layout-->


collapseColumns: collapse columns attribute is used to collapse or invisible the column’s of a table layout. These columns are the part of the table information but are invisible.
If the values is 0 then the first column appears collapsed, i.e it is the part of table but it is invisible.
Below is the example code of collapse columns with explanation included in which we collapse the first column of table means first column is an part of table but it is invisible

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns="0"> <!-- collapse the first column of the table row-->


    <!-- first row of the table layout-->
    <TableRow
        android:id="@+id/simpleTableLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">


A Complete example of the TableLayout



<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

   
android:layout_width="fill_parent"
   
android:layout_height="match_parent"
   
android:stretchColumns="0,1,2"
   
android:gravity="center">

    <TableRow
       
android:background="#FFFFFF"
       
android:layout_width="fill_parent"
       
android:layout_height="0dp"
       
android:layout_margin="1dp"
       
android:layout_weight="1"
       
>
        <TableRow
           
android:background="#000000"
           
android:layout_width="fill_parent"
           
android:layout_height="0dp"
           
android:layout_margin="1dp"
           
android:layout_weight="1"
           
>



        </TableRow>
    </TableRow>
    <TableRow
       
android:background="#000000"
        
android:layout_width="fill_parent"
       
android:layout_height="0dp"
       
android:layout_margin="1dp"
       
android:layout_weight="1"

       
>

        <TextView
           
android:layout_width="match_parent"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Date "
           
android:layout_margin="1dp"
           
android:layout_column="0"
           
android:background="#FFFFFF"
           
android:textStyle="bold"
           
android:gravity="center"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text="Miles "
           
android:layout_margin="1dp"
           
android:layout_column="1"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
android:textStyle="bold"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text="Calories"
           
android:layout_margin="1dp"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
android:textStyle="bold"
           
android:layout_column="2"
           
/>
    </TableRow>

    <TableRow
       
android:background="#000000"
       
android:layout_width="fill_parent"
       
android:layout_height="0dp"
       
android:layout_margin="1dp"
       
android:layout_weight="1"
       
>

        <TextView
           
android:layout_width="match_parent"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:layout_column="0"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:layout_column="1"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
android:layout_column="2" />
    </TableRow>



    <TableRow
       
android:background="#000000"
       
android:layout_width="fill_parent"
       
android:layout_height="0dp"
       
android:layout_margin="1dp"
       
android:layout_weight="1"
       
>

        <TextView
           
android:layout_width="match_parent"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:layout_column="0"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
            
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:layout_column="1"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
android:layout_column="2" />
    </TableRow>

    <TableRow
       
android:background="#000000"
       
android:layout_width="fill_parent"
        
android:layout_height="0dp"
       
android:layout_margin="1dp"
       
android:layout_weight="1"
       
>

        <TextView
           
android:layout_width="match_parent"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:layout_column="0"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
           
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:layout_column="1"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
/>

        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="match_parent"
            
android:textAppearance="?android:attr/textAppearanceLarge"
           
android:text=" Text"
           
android:layout_margin="1dp"
           
android:background="#FFFFFF"
           
android:gravity="center"
           
android:layout_column="2" />
    </TableRow>


</TableLayout>

And the Output will be look like that





Below is an other example of TableLayout which is used to create a login Form.


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical"
    android:stretchColumns="1">

    <TableRow android:padding="5dip">



        <TextView
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_span="2"
            android:gravity="center_horizontal"
            android:text="@string/loginForm"
            android:textColor="#0ff"
            android:textSize="25sp"
            android:textStyle="bold" />

    </TableRow>

    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_marginLeft="10dp"
            android:text="@string/userName"
            android:textColor="#fff"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/userName"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_marginLeft="10dp"
            android:background="#fff"
            android:hint="@string/userName"
            android:padding="5dp"
            android:textColor="#000" />
    </TableRow>

    <TableRow>
        <TextView android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="@string/password"
            android:textColor="#fff"
            android:textSize="16sp" />

        <EditText

            android:id="@+id/password"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:background="#fff"
            android:hint="@string/password"
            android:padding="5dp"
            android:textColor="#000" />
    </TableRow>


    <TableRow android:layout_marginTop="20dp">



        <Button
            android:id="@+id/loginBtn"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_span="2"
            android:background="#0ff"
            android:text="@string/login"
            android:textColor="#000"
            android:textSize="20sp"
            android:textStyle="bold" />

    </TableRow>

</TableLayout>


And the output screen will be look like