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
to the TextView, but that only caused the entire view to become black.
<TextView
android:background="@android:color/green"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableTop="@android:color/black"
android:drawableBottom="@android:color/black"
android:text="la la la" />
Is there a way to easily add a top and bottom border to a View (in particular, a TextView) in Android?
Here is a way to achieve it.
First item for stroke and second one for solid background. Hiding left and right borders.
Just to enforce @phreakhead ´s and user1051892 ´s answers,
<item android:bottom|android:left|android:right|android:top>
if negative, must to be greater than<stroke android:width>
. If not, item´s painting will be mixed with stroke´s painting and you may think these values are not working.Option 1: Shape Drawable
This is the simplest option if you want a border around a layout or view in which you can set the background. Create an XML file in the
drawable
folder that looks something like this:You can remove the
solid
if you don't want a fill. The setbackground="@drawable/your_shape_drawable"
on your layout/view.Option 2: Background View
Here's a little trick I've used in a
RelativeLayout
. Basically you have a black square under the view you want to give a border, and then give that view some padding (not margin!) so the black square shows through at the edges.Obviously this only works properly if the view doesn't have any transparent areas. If it does I would recommend you write a custom
BorderView
which only draws the border - it should only be a few dozen lines of code.If you're wondering, it does work with
adjustViewBounds=true
. However, it doesn't work if you want to have a background in an entireRelativeLayout
, because there is a bug that stops you filling aRelativeLayout
with aView
. In that case I'd recommend theShape
drawable.Option 3: 9-patch
A final option is to use a 9-patch drawable like this one:
You can use it on any view where you can set
android:background="@drawable/..."
. And yes it does need to be 6x6 - I tried 5x5 and it didn't work.The disadvantage of this method is you can't change the colours very easily, but if you want fancy borders (e.g. only a border at the top and bottom, as in this question) then you may not be able to do them with the
Shape
drawable, which isn't very powerful.Option 4: Extra views
I forgot to mention this really simple option if you only want borders above and below your view. You can put your view in a vertical
LinearLayout
(if it isn't already) and then add emptyView
s above and below it like this:First make a xml file with contents shown below and name it border.xml and place it inside the layout folder inside the res directory
After that inside the code use
This will make a black line on top and bottom of the TextView.
So I wanted to do something slightly different: a border on the bottom ONLY, to simulate a ListView divider. I modified Piet Delport's answer and got this:
Note using px instead of dp to get exactly 1 pixel divider (some phone DPIs will make a 1dp line disappear).
Try wrapping the image with a linearlayout, and set it's background to the border color you want around the text. Then set the padding on the textview to be the thickness you want for your border.