How to Create a Table with Borders in Android

How to add border around TableLayout?

In order to create a border around your table rows and around the table layout, you need to create a drawable to serve as a border and then set it as a background to your rows.

For example:

res/drawable/border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle">
<solid android:color="#ffffff"/>
<stroke android:width="1dp" android:color="#000000"/>
</shape>

res/layout/your_layout.xml

<TableLayout
android:id="@+id/table2"
android:layout_width="fill_parent"
android:layout_below="@+id/test_button_text23"
android:layout_marginLeft="45dp"
android:layout_marginBottom="25dp"
android:layout_marginRight="45dp"
android:layout_height="fill_parent"
android:stretchColumns="*">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border">

<TextView
android:gravity="left"
android:text="Quantity"
android:background="@drawable/border"
android:textStyle="bold"/>

<TextView
android:gravity="center"
android:textStyle="bold"
android:background="@drawable/border"
android:text="Item" />

</TableRow>

</TableLayout>

This won't look exactly like the picture you posted, but play with it to get what you want.

How to create a dynamic table with borders in android

You can try to define a style xml file and use it for your table using setBackgroundDrawable() function.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#99FFFFFF"/>
<corners android:radius="30px"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

How to give border of cells in tablelayout in android

there is no direct way of doing this. you have to mess around with background, margins and padding.

i ended up using something like this

<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" android:background="#ff0000">
<TableRow android:background="#00ff00" android:layout_margin="2dip">
<Button android:id="@+id/button" android:text="+" android:background="#0000ff" android:layout_margin="2dip"/>
<TextView android:text="@string/label" android:background="#0000ff" android:layout_margin="2dip"/>
<TextView android:id="@+id/amount" android:background="#0000ff" android:layout_margin="2dip"/>
</TableRow>

taken from here. LINK

Border for table row items in android

Thanks @idunnololz.
But your solution doesn't work in big screens. b'z of margin is only 2dp and if i increase it for other screen sizes it will effect required ui.

And in last row i don't know why it takes much more space then all above row.
like below image

weird output

so at last i have given fixed height and width for textviews. if you have better solution then please let me know.

<style name="TextViewMyTheme" parent="TextAppearance.AppCompat.Title">
<item name="android:layout_height">60dp</item>
<item name="android:layout_width">50dp</item>
<item name="android:minHeight">48dip</item>
<item name="android:textSize">@dimen/text_size_28sp</item>
<item name="android:gravity">center</item>
<item name="android:background">?attr/selectableItemBackground</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:padding">@dimen/padding_10dp</item>
</style>

Custom Table Android view with borders

This is the exact xml you need:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_weight="2">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4">

<RelativeLayout
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_gravity="bottom"
android:gravity="bottom"
android:padding="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:padding="5dp">
<TextView
android:text="1"/>
<TextView
android:text="2"/>
<TextView
android:text="3"/>
<TextView
android:text="4"/>
<TextView
android:text="5"/>
<TextView
android:text="6"/>
<TextView
android:text="7"/>
<TextView
android:text="8"/>
<TextView
android:text="9"/>


</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
<TextView
android:text="6"/>
<TextView
android:text="7"/>
<TextView
android:text="3"/>
<TextView
android:text="4"/>
<TextView
android:text="5"/>
<TextView
android:text="8"/>
<TextView
android:text="1"/>
<TextView
android:text="2"/>
</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
<TextView
android:text="6"/>
<TextView
android:text="7"/>
<TextView
android:text="3"/>
<TextView
android:text="4"/>
<TextView
android:text="5"/>
<TextView
android:text="8"/>
<TextView
android:text="1"/>
<TextView
android:text="2"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black"></View>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">

<RelativeLayout
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_gravity="bottom"
android:gravity="bottom"
android:padding="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:padding="5dp">
<TextView
android:text=""/>
</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black"></View>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:padding="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:padding="5dp">
<TextView
android:text="1"/>
<TextView
android:text="2"/>
<TextView
android:text="3"/>
<TextView
android:text="4"/>
<TextView
android:text="5"/>
<TextView
android:text="6"/>
<TextView
android:text="7"/>
<TextView
android:text="8"/>
<TextView
android:text="9"/>


</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
<TextView
android:text="6"/>
<TextView
android:text="7"/>
<TextView
android:text="3"/>
<TextView
android:text="4"/>
<TextView
android:text="5"/>
<TextView
android:text="8"/>
<TextView
android:text="1"/>
<TextView
android:text="2"/>
</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
<TextView
android:text="6"/>
<TextView
android:text="7"/>
<TextView
android:text="3"/>
<TextView
android:text="4"/>
<TextView
android:text="5"/>
<TextView
android:text="8"/>
<TextView
android:text="1"/>
<TextView
android:text="2"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black"></View>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:padding="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:padding="5dp">
<TextView
android:text=""/>
</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
</TableRow>

<TableRow
android:padding="5dp">
<TextView
android:text="5"/>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

</LinearLayout>



</LinearLayout>

Sample Image

How to add border to simple tablelayout programmatically

Create Gradient into res\xml\table.xml like:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#C0C0C0"
android:endColor="#505050"
android:angle="90"/>
<corners android:radius="2px" />
</shape>

And set to your TableLayout background

TableLayout table = (TableLayout)findViewById(R.id.tableLayout1);
table.setBackgroundDrawable(getResources().getDrawable(R.xml.table));

And Programmatically you can achieve like:

  GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {Color.parseColor("#C0C0C0"), Color.parseColor("#505050")});
gd.setGradientCenter(0.f, 1.f);
gd.setLevel(2);
table.setBackgroundDrawable(gd);


Related Topics



Leave a reply



Submit