Android text underline XML layout | TextView Kotlin/Java

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>.

Underline text Programmatically

This can only be used from code, not XML.

Kotlin Code

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”.

Android TextView Underline xml layout

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.

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.

Output screenshot Android Text Underline example:

Android text underline

Download source code of Android text underline in Kotlin

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.

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)
TargetSdkVersion=”28″Coding in Kotlin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.