2 ways to make text underline in Android example

Last updated Apr 24, 2021


In this post i will cover how can we define text underline in android xml file. In this example i will show you make text under line in 2 ways.  By Programatically and by Using Html String. So let's create underline text android application.

Let's get started

Step 1: Create android Application in Android studio

Step 2:  Create an xml file add below code

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:padding="8dp"
        android:text="RRTutors"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="48sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:clickable="true"
        android:focusable="true"
        android:text="Click to have a underlined text"
        android:textColor="@color/teal_700"
        android:textSize="24sp"
        android:textStyle="bold|italic" />
</RelativeLayout>

 

1) By using Kotlin Spannable String

Step 3: Update activity code with below code

package com.rrtutors.kotlincource

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableString
import android.text.style.UnderlineSpan
import android.widget.TextView

class TextUnderline : AppCompatActivity() {
    private val string = "This is an underlined textView!"
    lateinit var textView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_text_underline)
        title = "KotlinApp"
        textView = findViewById(R.id.textView)
        textView.setOnClickListener {
            val spannableString = SpannableString(string)
            spannableString.setSpan(UnderlineSpan(), 0, spannableString.length, 0)
            textView.text = spannableString
            textView.setTextColor(Color.BLUE)
        }
    }
}

 

Step 4: Run application

 

 

2) By Using HTML String

<string name = "underline"> <u>This is an underlined textView with HTML </u> </string>

 

Now add this string name to textview inside xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:padding="8dp"
        android:text="RRTutors"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="48sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:clickable="true"
        android:focusable="true"
        android:text="@string/underline"
        android:textColor="@color/teal_700"
        android:textSize="24sp"
        android:textStyle="bold|italic" />
</RelativeLayout>

 

 

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1594 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions