The Wayback Machine - https://web.archive.org/web/20240116225722/https://www.geeksforgeeks.org/how-to-underline-text-in-textview-in-android/
Open In App
Related Articles

How to Underline Text in TextView in Android?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In Android, a TextView is a primary UI element used to display text present in the form of characters, numbers, strings, and paragraphs. However, styles cannot be applied to text present in the TextView. So in this article, we will show you how you could underline text in a TextView in Android. Follow the below steps once the IDE is ready.

TextView in Android

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView as shown below.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/text_view_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package org.geeksforgeeks.underlinetext
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableString
import android.text.style.UnderlineSpan
import android.widget.TextView
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing 
        // the TextView from layout
        val mTextView = findViewById<TextView>(R.id.text_view_1)
  
        // Declaring a string
        val mString = "Hello Geek!"
        
        // Creating a Spannable String 
        // from the above string
        val mSpannableString = SpannableString(mString)
          
        // Setting underline style from 
        // position 0 till length of
        // the spannable string
        mSpannableString.setSpan(UnderlineSpan(), 0, mSpannableString.length, 0)
  
        // Displaying this spannable
        // string in TextView
        mTextView.text = mSpannableString
    }
}


Output:

You can see that the string is now underlined.

Output


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 06 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads