Android Layout
Underline Text
Android Development
Mobile App Design
Text Formatting

Can I underline text in an Android layout?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, you can underline text in Android, but there is no single dedicated XML attribute such as android:underline="true" for a normal TextView. The common solutions are to use styled text resources, Spannable text in code, or paint flags when you want to underline all of the text dynamically.

Use a String Resource for Simple Static Underlines

If the text is fixed and you want the whole string underlined, a styled string resource is often 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 UI labels, but it is limited when only part of the string should be underlined or the text changes at runtime.

Use SpannableString for Partial or Dynamic Underlining

For most real applications, SpannableString is the more 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 the Tap here part of the string.

If you want the entire string underlined:

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

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

Use Paint Flags When the Whole Text Should Always Be Underlined

Another option is to set the underline paint flag on the TextView.

kotlin
textView.paintFlags = textView.paintFlags or Paint.UNDERLINE_TEXT_FLAG

This applies to the whole rendered text. It is concise, but less flexible than spans because it does not let you underline only selected ranges.

It is useful when:

  • the entire text should always be underlined
  • the text changes frequently
  • you do not need range-level formatting

Many developers reach for underlines because the text is meant to look clickable. If that is the real intent, consider using a clickable span or proper link styling instead of only an underline.

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 you actual interaction semantics rather than just visual decoration.

XML Alone Has Limits

A common source of confusion is expecting XML layout attributes alone to handle every text decoration case. Android XML is good for static configuration, but once you need conditional formatting, mixed styles, or partial underline ranges, the right tool is usually code or a binding adapter.

So the answer is yes, but the method depends on whether the underline is:

  • static versus dynamic
  • whole-text versus partial-text
  • decorative versus interactive

Common Pitfalls

  • Looking for a direct TextView underline attribute in XML leads nowhere because normal TextView does not provide one.
  • Using styled string resources for dynamic text becomes awkward when only part of the content should be underlined.
  • Applying Paint.UNDERLINE_TEXT_FLAG when only a substring should be underlined is the wrong tool; use spans instead.
  • Underlining clickable text without making it actually clickable creates a misleading UI.
  • Mixing HTML-style text, spans, and manual paint flags without a clear reason can make formatting behavior harder to maintain.

Summary

  • Yes, Android can underline text, but the method depends on the use case.
  • Use styled string resources for simple static full-text underlines.
  • Use SpannableString and UnderlineSpan for partial or dynamic underlining.
  • Use paint flags when the entire TextView should always be underlined.
  • If the underline implies interaction, consider clickable spans or proper link behavior instead of decoration alone.

Course illustration
Course illustration

All Rights Reserved.