You can define the underlined text in an Android layout XML using a String Resouce XML file. In string res file you have to use an HTML underline tag <u> </u>
.
Another way is to underline text in TextView android programmatically.
String resource XML
If you are using a string resource XML file (supports HTML tags), it can be done using<b> </b>
, <i> </i>
and <u> </u>
.
1 2 3 4 5 |
<resources> <string name="your_string_here"> This is an <u>underline</u>. </string> </resources> |
Underline text Programmatically
This can only be used from code, not XML.
Kotlin Code
1 |
textView2.setPaintFlags(textView1.getPaintFlags() or Paint.UNDERLINE_TEXT_FLAG) |
Java Code
textView1.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Build an Android app Underline text:
We will see the code for underline text in an Android layout using XML resource file and Programmatically code approach.
Step 1. Create a new project “Build Your First Android App in Kotlin“
Step 2. Add a string in the XML file and add the below code.
File Location: res/values/strings.xml (open the file and new strings – “ul_string_here”.

1 2 3 4 |
<resources> <string name="app_name">Android text underline</string> <string name="ul_string_here">This is an <u>underline</u> using a string resource xml file </string> </resources> |
Step 3. Add following code in “activity_main.xml“
Adding 2 TextView in UI. One TextView is for XML way and another one is for a programmatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ul_string_here" android:layout_margin="10dp" android:textSize="20sp"/> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="UnderLine programmatically " android:layout_margin="10dp" android:textSize="20sp"/> </LinearLayout> |
Step 3. Add the following code in “MainActivity.kt“
This code for textview2 in Kotlin, if your app in Java then you can use java code as above.
1 |
package com.eyehunts.androidtextunderline<br><br>import android.graphics.Paint<br>import android.os.Bundle<br>import androidx.appcompat.app.AppCompatActivity<br>import kotlinx.android.synthetic.main.activity_main.*<br><br>class MainActivity : AppCompatActivity() {<br><br> override fun onCreate(savedInstanceState: Bundle?) {<br> super.onCreate(savedInstanceState)<br> setContentView(R.layout.<em>activity_main</em>)<br><br> textView2.setPaintFlags(textView1.getPaintFlags() or Paint.<em>UNDERLINE_TEXT_FLAG</em>)<br> }<br>}<br> |
Output screenshot Android Text Underline example:

Download source code of Android text underline in Kotlin
https://github.com/EyeHunts/AndroidTextUnderline
Q: How to do Android TextView underline like EditText?
Answer: Below text Display underlines in TextView the same as EditText use below code in Java.
1 2 |
TextView textView = (TextView) findViewById(R.id.textview1); textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); |
Do comment if you have any doubts and suggestions on this tutorial. If you know any other to do it, then also comment below.
Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=”25″
TargetSdkVersion=”28″Coding in Kotlin