Reputation: 332
Is it possible for an Android Tablelayout to display all dividers (see picture below) using the xml attributes?
I've spend a bit of time searching the internet and find that a lot of people just use customised views.
Is there any way to do this in xml or another better method?
example(don't mind the varying border thickness):
Upvotes: 3
Views: 6370
Reputation: 1704
In my case, I used this method.
I set Table background to black, then set table row background white with TopMargin.
In .xml :
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000" />
In .java :
TableRow tr = new TableRow(MainActivity.this);
tr.setBackgroundColor(resource.getColor(R.color.white));
int topMargin = 2; // border Thickness
TableLayout.LayoutParams tableRowParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, 250); // 250 is my row height
tableRowParams.setMargins(0, topMargin, 0, 0);
tr.setLayoutParams(tableRowParams);
Upvotes: 2
Reputation: 563
The simplest way, off the top of my head, to give you the lines you seek is to have each cell of the TableLayout or GridLayout be some container containing the widget(s) for that cell, where you give the container a background that is your line. A ShapeDrawable could be defined in XML for that background, which will be nicely resizeable based upon the actual requirements of the cell.
Upvotes: 1
Reputation: 1215
you can do this by using the following code
Create a selector xml in drawable folder with below code
valuecellborder.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<!-- <solid android:color="#dc6888"/> -->
<stroke android:width="0.1dp" android:color="#ffffff"
/>
<padding android:left="0dp" android:top="0dp"
android:right="0dp" android:bottom="0dp" />
</shape>
Now in your layout xml code in TableRow tag put
android:background="@drawable/valuecellbroder"
in every item tag such as Textview background
Upvotes: 5