Android Development
TextView
User Interface Design
Coding Tips
Android Layouts

How to add a border to an Android TextView

Codemia Knowledge Hub ·

Assign a shape drawable with a stroke as the TextView background. That keeps the styling in resources, makes it reusable, and works better than faking a border with extra wrapper views.

Shape drawable in XML for a fixed border

For a fixed border style, XML is the cleanest option. Create a drawable resource that defines both the fill and the stroke.

xml
1<!-- res/drawable/textview_border.xml -->
2<shape xmlns:android="http://schemas.android.com/apk/res/android"
3    android:shape="rectangle">
4
5    <solid android:color="@android:color/white" />
6    <stroke
7        android:width="1dp"
8        android:color="#222222" />
9    <corners android:radius="6dp" />
10    <padding
11        android:left="12dp"
12        android:top="8dp"
13        android:right="12dp"
14        android:bottom="8dp" />
15</shape>

Then apply it to the TextView.

xml
1<TextView
2    android:id="@+id/statusText"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:background="@drawable/textview_border"
6    android:text="Ready" />

This is the most maintainable approach when the border does not change dynamically.

GradientDrawable in code for dynamic borders

If the border color, width or radius changes at runtime, create the background programmatically.

kotlin
1val statusText = findViewById<TextView>(R.id.statusText)
2val density = resources.displayMetrics.density
3
4val background = GradientDrawable().apply {
5    shape = GradientDrawable.RECTANGLE
6    setColor(Color.WHITE)
7    setStroke((1 * density).roundToInt(), Color.parseColor("#D32F2F"))  // 1dp
8    cornerRadius = 6 * density                                         // 6dp
9}
10
11statusText.background = background

setStroke and cornerRadius take raw pixels, not dp, so multiply by displayMetrics.density (and import kotlin.math.roundToInt) to match the 1dp and 6dp of the XML version. A hard-coded setStroke(2, ...) is only 1dp wide on a 2x screen and looks thinner on denser ones.

This suits states such as validation errors, selected chips or live status indicators.

Balance stroke, radius and padding

Borders often look wrong not because of the stroke but because the text sits too close to the edge. If the drawable already defines padding, avoid doubling it with large android:padding values on the TextView itself.

A good result depends on three things together:

  • stroke width
  • corner radius
  • inner spacing around the text

Testing only the stroke color is usually not enough.

Stateful borders with selectors

If the border should change for pressed, selected or focused states, use a selector that swaps drawables.

xml
1<!-- res/drawable/textview_stateful_background.xml -->
2<selector xmlns:android="http://schemas.android.com/apk/res/android">
3    <item android:state_selected="true" android:drawable="@drawable/textview_border_selected" />
4    <item android:drawable="@drawable/textview_border" />
5</selector>

Then toggle the state in code or through view state changes. This is cleaner than rebuilding the background every time a small state changes.

When a Material component fits better

If the app already uses Material Components, a plain TextView with a custom background is often still enough. But if the control behaves more like a chip, badge or outlined input, use a more specialized component instead of styling TextView beyond recognition. That keeps UI code easier to maintain and accessibility behavior more predictable.

Keep border resources reusable

If several screens need the same outlined label, keep the drawable generic and vary only theme colors or state selectors. Centralizing border resources avoids small visual mismatches that accumulate across layouts.

Mistakes that make borders look wrong

  • Using nested layout wrappers to simulate a border instead of a drawable background.
  • Forgetting padding, which makes the text look cramped inside the border.
  • Hard-coding colors in several places instead of using reusable drawables or theme values.
  • Rebuilding drawable objects repeatedly when a selector resource would handle simple states.
  • Styling a TextView to mimic a different component when a more appropriate Material widget already exists.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms