I have a TextView and I\'d like to add a black border along its top and bottom borders. I tried adding android:drawableTop
and android:drawableBottom
Just to add my solution to the list..
I wanted a semi transparent bottom border that extends past the original shape (So the semi-transparent border was outside the parent rectangle).
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#33000000" /> <!-- Border colour -->
</shape>
</item>
<item android:bottom="2dp" >
<shape android:shape="rectangle" >
<solid android:color="#164586" />
</shape>
</item>
</layer-list>
Which gives me;
The currently accepted answer doesn't work. It creates thin vertical borders on the left and right sides of the view as a result of anti-aliasing.
This version works perfectly. It also allows you to set the border widths independently, and you can also add borders on the left / right sides if you want. The only drawback is that it does NOT support transparency.
Create an xml drawable named /res/drawable/top_bottom_borders.xml
with the code below and assign it as a TextView's background property.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#DDDD00" /> <!-- border color -->
</shape>
</item>
<item
android:bottom="1dp"
android:top="1dp"> <!-- adjust borders width here -->
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" /> <!-- background color -->
</shape>
</item>
</layer-list>
Tested on Android KitKat through Marshmallow
My answers is based on @Emile version but I use transparent color instead of solid.
This example will draw a 2dp bottom border.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<stroke android:width="2dp"
android:color="#50C0E9" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:bottom="2dp" >
<shape android:shape="rectangle" >
<stroke android:width="2dp"
android:color="@color/bgcolor" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
@color/bgcolor is the color of the background on wich you draw your view with border.
If you want to change the position of the border change the offset with one of:
android:bottom="2dp"
android:top="2dp"
android:right="2dp"
android:left="2dp"
or combine them to have 2 or more borders:
android:bottom="2dp" android:top="2dp"
Just as @Nic Hubbard said, there is a very easy way to add a border line.
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" >
</View>
You can change the height and background color to whatever you want.
You can also use a 9-path to do your job. Create it so that colored pixel do not multiply in height but only the transparent pixel.
To add a 1dp
white border at the bottom only and to have a transparent background you can use the following which is simpler than most answers here.
For the TextView
or other view add:
android:background="@drawable/borderbottom"
And in the drawable
directory add the following XML, called borderbottom.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-2dp" android:left="-2dp" android:right="-2dp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#ffffffff" />
<solid android:color="#00000000" />
</shape>
</item>
</layer-list>
If you want a border at the top, change the android:top="-2dp"
to android:bottom="-2dp"
The colour does not need to be white and the background does not need to be transparent either.
The solid
element may not be required. This will depend on your design (thanks V. Kalyuzhnyu).
Basically, this XML will create a border using the rectangle shape, but then pushes the top, right and left sides beyond the render area for the shape. This leaves just the bottom border visible.