Skip to content

Android text underline XML layout | TextView Kotlin/Java

  • by

You can define the underlined text in an Android layout XML using a String Resouce XML file. In a 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>.

<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

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

Android TextView Underline xml layout

    Android text underline
    This is an underline using a string resource xml file 


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.

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

package com.eyehunts.androidtextunderline

import android.graphics.Paint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

textView2.setPaintFlags(textView1.getPaintFlags() or Paint.UNDERLINE_TEXT_FLAG)
}
}

Output screenshot Android Text Underline example:

Android text underline

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *