Programmatically set border for TableRow array

Bigflow picture Bigflow · Nov 22, 2012 · Viewed 14.9k times · Source

I know it is the same question as here But it hasn't receive an answer yet, so I try it here, becuase I need it too :)
I got an Array: (I shortend the array/code for SO)

ScrollView sv = new ScrollView(this);
TableLayout ll=new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
TableRow tbrow=new TableRow(this);
 for(int i=0;i<mConnector.idArray.size();i++) {
         tbrow=new TableRow(this);
         tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
         tbrow.setBackgroundColor(Color.rgb(51, 51, 51));

         ll.addView(tbrow);
 }

hsv.addView(ll);
sv.addView(hsv);
setContentView(sv);

Left out the information in the Array, I don't think you need that.
But how to add borders in every row (prefer horizontal and vertical)? I hoped this was the solution:

tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
tbrow.setBackgroundColor(Color.rgb(51, 51, 51));

But it just colors my whole table grey.

Hope I am clear enough, and hope their is a solution.

Answer

user picture user · Nov 22, 2012

But how to add borders in every row (prefer horizontal and vertical)? I hoped this was the solution:

If you just want to have borders around your TableRows you can simply use the drawable below as the background for the TableRow:

R.drawable.row_border :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ffffff" />
    <stroke android:width="3dp" android:color="#99cc00" />

</shape>

then:

tbrow.setBackgroundResource(R.drawable.row_borders);

If you want to obtain a constant width border, you can use instead a layer-list with three versions, one for the top row, one for the rows in the middle and one for the bottom row.