How to put a border around an Android TextView?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The usual way to draw a border around a TextView in Android is to assign a shape drawable as its background. That keeps the styling in resources, makes it reusable, and works better than trying to fake a border with extra wrapper views.
Use a Shape Drawable in XML
For a fixed border style, XML is the cleanest option. Create a drawable resource and define both the fill and stroke.
Then apply it to the TextView.
This is the most maintainable approach when the border does not change dynamically.
Use a GradientDrawable in Code for Dynamic Styling
If border color, width, or radius changes at runtime, create the background programmatically.
This is useful for states such as validation errors, selected chips, or live status indicators.
Remember Padding and Background Interaction
Borders often look wrong not because the stroke is wrong, but because the text sits too close to the edge. If the drawable already defines padding, be careful not to double it with large android:padding values on the TextView itself.
A good visual result usually depends on three things together:
- stroke width
- corner radius
- inner spacing around the text
Testing only the stroke color is usually not enough.
Handle Stateful Borders with Selectors
If the border should change for pressed, selected, or focused states, use a selector that swaps drawables.
Then toggle the state in code or through view state changes. This is cleaner than rebuilding the background every time a small state changes.
Material Components Alternative
If the app already uses Material Components, sometimes a plain TextView with a custom background is still enough. But if the control is behaving more like a chip, badge, or outlined input, consider using a more specialized component instead of styling TextView beyond recognition.
That keeps your UI code easier to maintain and makes accessibility behavior more predictable.
Keep Border Resources Reusable
If several screens need the same outlined label style, keep the drawable generic and vary only theme colors or state selectors. Centralizing border resources avoids small visual mismatches that accumulate over time across multiple layouts.
That also simplifies maintenance.
Common Pitfalls
- Using nested layout wrappers just to simulate a border instead of a drawable background.
- Forgetting padding, which makes the text appear 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
TextViewto mimic a different component when a more appropriate Material widget already exists.
Summary
- The standard Android solution is a shape drawable assigned as the
TextViewbackground. - Use XML for fixed styles and
GradientDrawablefor runtime changes. - Balance stroke, corner radius, and padding together for a clean result.
- Use selectors when the border needs state-based styling.
- Prefer the right component if the UI starts behaving like more than a simple
TextView.

