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.
Then apply it to the TextView.
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.
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.
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
TextViewto mimic a different component when a more appropriate Material widget already exists.
Related reading
- How to quit android application programmatically
- How to quit or close single simulator from opened multiple simulator in Xcode 9?
- How to re-sign the ipa file?
- how to read value from string.xml in android?
- How to read/write a boolean when implementing the Parcelable interface?
- How to reduce iOS AVPlayer start delay
- How to reduce tensorflow size for android?
- How to refresh Android listview?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.