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?
To change this:
I prefer this approach in "drawable/top_bottom_border.xml":
This only makes the borders, not a rectangle that will appear if your background has a color.
Write down below code
Simplest way to add borders to inset the borders using
InsetDrawable
,following will shoe top border only :Just Add this TextView below the text where you want to add the border
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.Tested on Android KitKat through Marshmallow