Android Layout
Underline Text
Android Development
Mobile App Design
Text Formatting

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:

xml
<string name="underlined_label"><u>Underlined Text</u></string>

Layout:

xml
1<TextView
2    android:id="@+id/title"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:text="@string/underlined_label" />

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:

kotlin
1val textView = findViewById<TextView>(R.id.title)
2val content = "Tap here to continue"
3val spannable = SpannableString(content)
4spannable.setSpan(UnderlineSpan(), 0, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
5textView.text = spannable

This underlines only Tap here. To underline the entire string:

kotlin
1val content = "Underlined Text"
2val spannable = SpannableString(content)
3spannable.setSpan(UnderlineSpan(), 0, content.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
4textView.text = spannable

Spans are usually the best method when text comes from user input, API responses or view-model data.

Whole view: Paint.UNDERLINE_TEXT_FLAG

kotlin
textView.paintFlags = textView.paintFlags or 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:

kotlin
1val text = SpannableString("Open Terms of Service")
2text.setSpan(object : ClickableSpan() {
3    override fun onClick(widget: View) {
4        // handle click
5    }
6}, 5, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
7
8textView.text = text
9textView.movementMethod = LinkMovementMethod.getInstance()

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 TextView underline attribute in XML: a normal TextView does not have one.
  • Using styled string resources for dynamic text where only part of the content should be underlined.
  • Applying Paint.UNDERLINE_TEXT_FLAG when 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.

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