Table Layout positions its children into rows and columns.The table will have as many columns as the row with the most cells. A table can leave cells empty, but cells cannot span columns, as they can in HTML.
In this tutorial we show you, how to use Table Layout and create Table as per requirement.

layout-2016-01-30-110607

Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#d3d3d3"
>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*"
>

<TableRow
android:id="@+id/tableRow_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#16a085">

<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_span="6"
android:gravity="center"
android:text="Data Base"
android:textColor="#ffffff"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="serif"></TextView>
</TableRow>  <TableRow
android:id="@+id/tableRow_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1abc9c"
android:padding="5dp"
android:gravity="center">

<TextView
android:id="@+id/TextView_serial"
android:text="SNo"
android:textColor="#000000"></TextView>

<TextView
android:id="@+id/TextView_item_name"
android:text="Item Name"
android:textColor="#000000"
android:typeface="serif"></TextView>

<TextView
android:id="@+id/TextView_quantity_no"
android:text="Quantity(Kg)"
android:textColor="#000000"
android:typeface="serif"></TextView>

<TextView
android:id="@+id/TextView_availability"
android:text="Available"
android:textColor="#000000"
android:typeface="serif"></TextView>
</TableRow> <TableRow
android:id="@+id/tableRow_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:id="@+id/TextView_serial_no"
android:text="1"></TextView>

<TextView
android:id="@+id/TextView_item"
android:text="Apple"
android:typeface="serif"></TextView>

<TextView
android:id="@+id/TextView_quantity"
android:text="10 kg"
android:typeface="serif"></TextView>

<TextView
android:id="@+id/TextView_available"
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:text="2"></TextView>

<TextView
android:text="orange"
android:typeface="serif"></TextView>

<TextView
android:text="20 kg"
android:typeface="serif"></TextView>

<TextView
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#ffffff">

<TextView
android:text="3"></TextView>

<TextView
android:text="Banana"
android:typeface="serif"></TextView>

<TextView
android:text="40 kg"
android:typeface="serif"></TextView>

<TextView
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow>  <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:text="4"></TextView>

<TextView
android:text="Apricot"
android:typeface="serif"></TextView>

<TextView
android:text="20 kg"
android:typeface="serif"></TextView>

<TextView
android:text="No"
android:typeface="serif"></TextView>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:text="5"></TextView>

<TextView
android:text="Blueberry"
android:typeface="serif"></TextView>

<TextView
android:text="60 kg"
android:typeface="serif"></TextView>

<TextView
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#ffffff">

<TextView
android:text="6"></TextView>

<TextView
android:text="Guava"
android:typeface="serif"></TextView>

<TextView
android:text="40 kg"
android:typeface="serif"></TextView>

<TextView
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:text="7"></TextView>

<TextView
android:text="Mango"
android:typeface="serif"></TextView>

<TextView
android:text="50 kg"
android:typeface="serif"></TextView>

<TextView
android:text="No"
android:typeface="serif"></TextView>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:text="8"></TextView>

<TextView
android:text="Papaya"
android:typeface="serif"></TextView>

<TextView
android:text="20 kg"
android:typeface="serif"></TextView>

<TextView
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow>  <TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#ffffff">

<TextView
android:text="9"></TextView>

<TextView
android:text="Watermelon"
android:typeface="serif"></TextView>

<TextView
android:text="10 kg"
android:typeface="serif"></TextView>

<TextView
android:text="No"
android:typeface="serif"></TextView>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:text="10"></TextView>

<TextView
android:text="orange"
android:typeface="serif"></TextView>

<TextView
android:text="20 kg"
android:typeface="serif"></TextView>

<TextView
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#bdc3c7">

<TextView
android:text="11"></TextView>

<TextView
android:text="Fig"
android:typeface="serif"></TextView>

<TextView
android:text="60 kg"
android:typeface="serif"></TextView>

<TextView
android:text="Yes"
android:typeface="serif"></TextView>
</TableRow>  </TableLayout>

</RelativeLayout>
Advertisements