How to underline text in textview

In Android, a TextView is the primary UI element that displays text in the form of characters, integers, strings, and paragraphs. However, styles cannot be applied to text within the TextView.

In this article, you will learn how to create your underline text Example of underline text in Android. So let’s take example of underline text  in Android of Mobile application.   

Example of Underline text

                                               

Step-by-Step Implementation 
​Step 1: Create a New Project in Android ​studio (File >new  project).


Step 2: Create an main_activity.xml file in your layout folder . path (res>layout) 

Implement the same in​voke the following code inside main_activity.xml file.  

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity3">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtcandidrootsolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome To Candidroot Solutions"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create an MainActivity.kt file in your layout folder 

Implement the same in​voke the following code inside MainActivity.kt file. 

 import android.annotation.SuppressLint
import android.os.Bundle
import android.text.SpannableString
import android.text.style.UnderlineSpan
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity3 : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main4)
val mTextView = findViewById<TextView>(R.id.txtcandidrootsolution)
val mString = "Welcome To Candidroot Solutions"
val mSpannableString = SpannableString(mString)
mSpannableString.setSpan(UnderlineSpan(), 0, mSpannableString.length, 0)
mTextView.text = mSpannableString
}
}
Step 4: Output of the above example.

                             

Happy coding!

365Bloggy July 26, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive