Edit: I have seen this question How can I create a table with borders in Android? and in fact the top answer is the solution I am using but has the problem in question.
I have a TableLayout
which I populate with lots of data dynamically. It is enclosed in a ScrollView
I wish to style it with a border so it looks like a spreadsheet. My solution thus far is to define a <shape>
and surround the dynamically-created TextView
s with the border. It works but you can see the discrepancy between the borders on each cell (each cell has it's own separate border). See image at the bottom.
Is there a better solution, so that it looks like one continuous spreadsheet?
Here is my XML for the table:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:layout_weight="1"
>
<TableLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tbl_statistics"
android:stretchColumns="*"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
</TableLayout>
</ScrollView>
Here is the XML for the border shape:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<stroke
android:width="1dip"
android:color="@color/material_blue_300" />
</shape>
And here is the code that dynamically generates the table:
for(StatisticRowItem rowItem : rowItems)
{
//add a new row to the TableLayout
TableRow row = new TableRow(this);
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView txtCol1 = new TextView(this);
txtCol1.setText(rowItem.getItem1());
txtCol1.setBackgroundResource(R.drawable.border_1dp);
txtCol1.setGravity(Gravity.CENTER_HORIZONTAL);
row.addView(txtCol1);
TextView txtCol2 = new TextView(this);
txtCol2.setText(rowItem.getItem2());
txtCol2.setGravity(Gravity.CENTER_HORIZONTAL);
txtCol2.setBackgroundResource(R.drawable.border_1dp);
row.addView(txtCol2);
tbl.addView(row);
//add a new line to the TableLayout:
final View vline = new View(this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 2));
//vline.setBackgroundColor(Color.BLUE);
tbl.addView(vline);
}