How to create a table by using TableLayout in Android Studio?

Ka Chun Yim picture Ka Chun Yim · Nov 30, 2015 · Viewed 76.4k times · Source

Can some one provides a sample code for me to reference? I want to create the table which likes the photo below.

TABLE:

enter image description here

Answer

Androider picture Androider · Nov 30, 2015

enter image description here

layout xml file for you just copy paste it and edit according to your choice:

<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>