Article Image
read
To draw borders we have to understand the basics of layer-list drawable component. An exhaustive guide is provided by Android Developers Guide.
Code snippet to draw top and bottom borders:
res/drawable/border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item><!--Item 1-->
<shape android:shape="rectangle">
<stroke android:color="@color/green" android:width="2dp"/>
<solid android:color="#FFFFFFFF" />
</shape>
</item>
<item android:top="2dp" android:bottom="2dp"><!--Item 2-->
<shape android:shape="rectangle">
<solid android:color="#FFFFFFFF" />
</shape>
</item>
</layer-list>
#How layer list works?
- Layer One
<item><!--Item 1-->
<shape android:shape="rectangle">
<stroke android:color="@color/green" android:width="2dp"/>
<solid android:color="#FFFFFFFF" />
</shape>
</item>
Above code snippet results in a layer with border on all sides. Stroke tag defines the border and Solid tag defines the background colour.
#
- Layer Two
<item android:top="2dp" android:bottom="2dp"><!--Item 2-->
<shape android:shape="rectangle">
<solid android:color="#FFFFFFFF" />
</shape>
</item>
This snippet is drawn over the first layer which has top offset 2dp and bottom offset 2dp (border thickness)