How do I put a border around an Android textview?


Is it possible to draw a border around a textview?

1/2/2017 12:35:02 AM

Accepted Answer

You can set a shape drawable (a rectangle) as background for the view.

<TextView android:text="Some text" android:background="@drawable/back"/>

And rectangle drawable back.xml (put into res/drawable folder):

<shape xmlns:android="" android:shape="rectangle" >
   <solid android:color="@android:color/white" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>

You can use @android:color/transparent for the solid color to have a transparent background. You can also use padding to separate the text from the border. for more information see:

4/3/2017 4:53:11 AM

Let me summarize a few different (non-programmatic) methods.

Using a shape drawable

Save the following as an XML file in your drawable folder (for example, my_border.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=""
    android:shape="rectangle" >

    <!-- View background color -->
        android:color="@color/background_color" >

    <!-- View border color and width -->
        android:color="@color/border_color" >

    <!-- The radius makes the corners rounded -->
        android:radius="2dp"   >


Then just set it as the background to your TextView:

    android:background="@drawable/my_border" />

More help:

Using a 9-patch

A 9-patch is a stretchable background image. If you make an image with a border then it will give your TextView a border. All you need to do is make the image and then set it to the background in your TextView.

    android:background="@drawable/my_ninepatch_image" />

Here are some links that will show how to make a 9-patch image:

What if I just want the top border?

Using a layer-list

You can use a layer list to stack two rectangles on top of each other. By making the second rectangle just a little smaller than the first rectangle, you can make a border effect. The first (lower) rectangle is the border color and the second rectangle is the background color.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="">

    <!-- Lower rectangle (border color) -->
        <shape android:shape="rectangle">
            <solid android:color="@color/border_color" />

    <!-- Upper rectangle (background color) -->
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/background_color" />

Setting android:top="2dp" offsets the top (makes it smaller) by 2dp. This allows the first (lower) rectangle to show through, giving a border effect. You can apply this to the TextView background the same way that the shape drawable was done above.

Here are some more links about layer lists:

Using a 9-patch

You can just make a 9-patch image with a single border. Everything else is the same as discussed above.

Using a View

This is kind of a trick but it works well if you need to add a seperator between two views or a border to a single TextView.

    android:orientation="vertical" >

        android:layout_height="wrap_content" />

    <!-- This adds a border between the TextViews -->
        android:background="@android:color/black" />

        android:layout_height="wrap_content" />


Here are some more links:

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow