Create table with border in android

Table Layout:

According to the Android developer guide, border lines are not displayed for table layouts. However, often you will find the need to do things such as having vertical separators between table cells and borders around table rows.

Give the TableLayout a background color, give the TableRow another background color and set margin to the TableRow. The amount of the margin is the amount of the “border”. Same for each View in the TableRow.

Code:


We can create table with border. Consider the following main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
<TableLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="*" android:background="#ff0000">
<TableRow 
          android:background="#00ff00" 
          android:layout_margin="2dip">
<TextView 
         android:text="1st Column"
          android:background="#0000ff"
          android:layout_margin="2dip"/>
<TextView 
             android:text="2nd Column" 
            android:background="#0000ff"
            android:layout_margin="2dip"/>

<TextView 
            android:id="@+id/amount" 
            android:background="#0000ff"
            android:layout_margin="2dip" 
            android:text="3rd col"/>
</TableRow>
</TableLayout>

Now the activity code is below:

Code:

package sample.my.tablelayout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;

public class Article_androidActivity extends Activity 
{

  @Override
      public void onCreate(Bundle savedInstanceState) 
       {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.main);
        }
}

0 comments:

Post a Comment