2

I am trying to create a table in android with borders but not able to achieve the task.

enter image description here

I am working with target SDK version 23.

Following is the code for my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/enquiryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:shrinkColumns="*"
    android:stretchColumns="*">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_span="4"
            android:background="#ffffff"
            android:gravity="center"
            android:text="Table 2"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="A"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="B"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="C"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="D"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="E"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="F"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="G"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="H"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="I"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="J"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="K"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="L"
            android:textColor="#000000" />
    </TableRow>
</TableLayout>
</RelativeLayout>

The problem is I am not able to display the borders in the table otherwise the table is created properly.
Please guide me to accomplish the task shown in the figure.

Swapnil Lanjewar
  • 646
  • 10
  • 26

2 Answers2

1

Create two xmls. One for the row & table and the other for columns.

table_row.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#000000"/>
</shape>

table.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="2dp" android:color="#515151"/>
    <corners android:radius="3dp" />
</shape>

Assign table.xml as background to TableLayout and TableRow tags. And for assign table_row.xml as background to your TextView tags.

Example:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:background="@drawable/table">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/table"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_span="4"
            android:background="@drawable/table_row"
            android:gravity="center"
            android:text="Table 2"

            android:textColor="#000000" />
    </TableRow>
</TableLayout>
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
0

After lots of searching and digging I made it working. Thank you guys for the support!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/enquiryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    android:layout_centerInParent="true"
    android:background="#000000"
    android:shrinkColumns="*"
    android:stretchColumns="*">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_span="4"
            android:background="#ffffff"
            android:gravity="center"
            android:text="Table 2"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="A"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="B"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="C"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="D"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="E"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="F"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="G"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="H"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="I"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="J"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="K"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="L"
            android:textColor="#000000" />
    </TableRow>
 </TableLayout>


</RelativeLayout>
Swapnil Lanjewar
  • 646
  • 10
  • 26