How to set border of GridView.
Such as Divider and DividerHeight of ListView.
Or how to display the border.
Here are some examples of borders in a GridView.
You can see where I defined the Red and Blue borders in my XML.
This is my main.xml Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="@color/blue"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
/>
</RelativeLayout>
The thickness of the Red border is controlled GridView's layout_margin
attribute and the Blue borders are controlled by horizontalSpacing
and verticalSpacing
.
To make the black cell backgrounds I used this layout and saved it as list_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
My Activity:
public class Example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] array = new String[] {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
List<String> list = new ArrayList<String>(Arrays.asList(array));
GridView grid = (GridView) findViewById(R.id.gridview);
grid.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
}
}
Create grid_row_border.xml in the res/drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<stroke
android:color="@color/material_grey_600"
android:width="1dp" />
</shape>
Now use this in you grid layout xml as below
android:background="@drawable/grid_row_border"
Provide padding [android:padding="5dp"] and margin [android:layout_margin="5dp"] to look better.
use below xml file as background in grid item xml file.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners
android:bottomRightRadius="12dp"
android:bottomLeftRadius="12dp"
android:topLeftRadius="12dp"
android:topRightRadius="12dp" />
<stroke
android:color="@android:color/white"
android:width="1dp" />
</shape>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With