Ali_Waris
Ali_Waris

Reputation: 2382

How to put border around set of table rows in Android?

I have read similar questions here, but none of them cleared my doubt.

I have read about adding border to each cell, table row, etc. But I need to have a border (kind of a box) around some set of rows.

Lets assume that a table has 5 rows, but i want to enclose 3 rows in a separate box and the other 2 rows in a separate box.

I want to do it programmatically from java code behind; is it possible? enter image description here

The rectangles having sphere in it are the elements of tablerow and i have set a green and red background to some set of tablerows. I want it to be shown just like border, like a box containing the set of rows instead of a complete red and green background. I hope I am clear with my question. Need your suggestions..

Upvotes: 0

Views: 2113

Answers (1)

MHP
MHP

Reputation: 2731

you can do it by set background and padding for your rows.of course I think still other ways exist.

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <TableRow
        android:id="@+id/t1"
        android:background="#00f"
        android:padding="2dp"
        android:layout_margin="5dp">
        <TextView
            android:text=" blue border "
            android:gravity="center"
            android:textColor="#000"
            android:background="#fff"
            android:padding="5dp"/>
    </TableRow>
    <TableRow
        android:id="@+id/t2"
        android:background="#f00"
        android:padding="2dp"
        android:layout_margin="5dp">
        <TextView
            android:text="red border"
            android:textColor="#000"
            android:gravity="center"
            android:background="#fff"
            android:padding="5dp"/>
    </TableRow>
    <TableRow
        android:id="@+id/t3"
        android:background="#0f0"
        android:padding="2dp"
        android:layout_margin="5dp">
        <TextView
            android:text="green border"
            android:textColor="#000"
            android:gravity="center"
            android:background="#fff"
            android:padding="5dp"/>
    </TableRow>
</TableLayout>  

enter image description here

UPDATE:

in your activity you can change border color by TabaleRow id:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TableRow row1 = (TableRow) findViewById(R.id.t1);
    row1.setBackgroundColor(Color.BLUE);

    TableRow row2 = (TableRow) findViewById(R.id.t2);
    row2.setBackgroundColor(Color.BLUE);

    TableRow row3 = (TableRow) findViewById(R.id.t3);
    row3.setBackgroundColor(Color.RED);

}

Upvotes: 2

Related Questions