underline a textview in android

Do you want to underline a TextView in Android?

In this post, you will learn how to underline a TextVew and also learn to change underline color.

You can download Java & Kotlin Source code too.

Okay, let’s get started.

1 – Underline a TextView in Android – Using Html.fromHtml()

underline a textview in android

It’s the simplest way. Html.fromHtml() method helps us to render HTML code in TextView.

But Html.fromHtml(resource : String) was deprecated in API 24. So now you can use HtmlCompat.fromHtml() method. It’s available in both android.support.v4.text.HtmlCompat, and androidx.core.text.HtmlCompat.

But androidx.core.text.HtmlCompat is the latest one. For that, you must include the dependency in gradle file.

underline a textview in android studio

1.2 Above underline method in a direct way

underline below textview android

This is for beginners. You can directly implement the code just like below.

1.3 – Underline TextView In Android Using String resource & HtmlCompat.fromHtml()

how to underline textview in android

In this method, we use strings.xml to store our HTML code and access using getString() method inside HtmlCompat.fromHtml() method.

The <u> tag doesn’t work with devices I checked. So I use &lt and; here. Just like below.


2. Underline Using Paint Flags

android textview underline

You can use Paint flags to make underline below TextView. Use Paint.UNDERLINE_TEXT_FLAG.

3.1 Underline using SpannableString

underline below textview android

You can underline TextView using SpannableString and setSpan() method with the help of UnderlineSpan().

3.2 Underline Using SpannableString

how to underline textview in android

You can underline wherever you want in a word or sentence. Just Specify the start and endpoints.

Here 4 is the start point and 13 is the endpoint.

4. Underline Using Layerlist Drawable

android textview underline color

You can use XML drawable to make underline. Here I am going to talk about LayerList Drawable.

Make a drawable named underline_layerlist.xml, and paste below code.

Change Underline color using android:color attribute.

Here I used solid element color as transparent. If not, the solid element color becomes black in older devices (below Lolipop).

Change underline width using android:width attribute.

5. Underline using Shape Drawable

android textview underline color

Using Line shape we can create Underline below TextView Just like this.

Here android:width and android:color are defines underline stroke and color just like layerlist drawable.

A Small Surprise – 6. Underline Using View element

android textview underline color

Here I just made a line using View element and place it below the TextView.

Create Android Studio Project – UnderlineTextView ex

Let’s create an Android Studio project with the above code examples.

Open your Android Studio,

Start a new Android Studio Project.

Application Name: UnderlineTextView Ex

Company domain: androidride.example.com

Check to include Kotlin support.

Select minimum SDK:API 15 – Android 4.0.3 (Ice Cream Sandwich) and click Next.

Next dialog, Select Empty Activity and click Next.

Activity Name: MainActivity
Check Generate layout file

Layout Name: activity_main

Click Finish.

copy paste below code in respective files.

build.gradle(Project: UnderlineTextView)









Okay. Let’s Run Now. You can use below links to Run your app.

underline a textview in android - android textview underline color

Yeah… That’s all for now.

Let me know if anything missing here.

Sharing is Caring.