How to underline text in an Android TextView
Codemia Knowledge Hub ·
Yes, but a normal TextView has no dedicated XML attribute such as android:underline="true". Use a <u> tag in a string resource for static text, a SpannableString with UnderlineSpan for partial or dynamic text, or Paint.UNDERLINE_TEXT_FLAG to underline the whole view in code.
Static text: a string resource with u tags
For fixed text where the whole string is underlined, a styled string resource is the simplest option.
strings.xml:
Layout:
This is easy to maintain for static labels, but it gets awkward when only part of the string should be underlined or the text changes at runtime.
Partial or dynamic text: SpannableString and UnderlineSpan
For most real applications, spans are the flexible answer:
This underlines only Tap here. To underline the entire string:
Spans are usually the best method when text comes from user input, API responses or view-model data.
Whole view: Paint.UNDERLINE_TEXT_FLAG
This underlines all rendered text. It is concise, but you cannot underline only selected ranges. It fits when:
- the entire text should always be underlined
- the text changes frequently
- you do not need range-level formatting
Underlined text that should be clickable
If the underline is there because the text should look clickable, make it actually clickable with a ClickableSpan:
That gives real interaction instead of just visual decoration.
Choosing between XML and code
XML layout attributes handle static configuration, but conditional formatting, mixed styles or partial underline ranges usually call for code or a binding adapter. Pick the method based on whether the underline is:
- static or dynamic
- whole-text or partial-text
- decorative or interactive
Mistakes to avoid
- Searching for a
TextViewunderline attribute in XML: a normalTextViewdoes not have one. - Using styled string resources for dynamic text where only part of the content should be underlined.
- Applying
Paint.UNDERLINE_TEXT_FLAGwhen only a substring should be underlined. Use spans instead. - Underlining text that looks clickable without making it clickable, which misleads users.
- Mixing HTML-style text, spans and paint flags without a clear reason, which makes formatting harder to maintain.
Related reading
- Can I use a UIRefreshControl in a UIScrollView?
- Can I use Objective-C blocks as properties?
- Can I use the range operator with if statement in Swift?
- Can Objective-C code call Swift class extensions?
- Can someone explain to me what the Builder Class does in Flutter?
- Can the Android drawable directory contain subdirectories?
- Can the Android layout folder contain subfolders?
- Can UIView be copied?
.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.