[android] To draw an Underline below the TextView in Android

I want to draw the underline below my TextView. I have searched a few content but couldn't find out anything fruitful.

Can anyone please help me out here?

This question is related to android textview underline

The answer is


If your TextView has fixed width, alternative solution can be to create a View which will look like an underline and position it right below your TextView.

<RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent">

        <TextView
            android:id="@+id/myTextView"
            android:layout_width="20dp"
            android:layout_height="wrap_content"/>

        <View
            android:layout_width="20dp"
            android:layout_height="1dp"
            android:layout_below="@+id/myTextView"
            android:background="#CCCCCC"/>
</RelativeLayout>

In Kotlin you can create extension property:

inline var TextView.underline: Boolean
    set(visible) {
        paintFlags = if (visible) paintFlags or Paint.UNDERLINE_TEXT_FLAG
        else paintFlags and Paint.UNDERLINE_TEXT_FLAG.inv()
    }
    get() = paintFlags and Paint.UNDERLINE_TEXT_FLAG == Paint.UNDERLINE_TEXT_FLAG

And use:

textView.underline = true

underline a textview in android

5 Amazing Ways To Underline A TextView In Android - Kotlin/Java & XML

  1. String html = "<u>Underline using Html.fromHtml()</u>"; textview.setText(Html.fromHtml(html));

But Html.fromHtml(String resource) was deprecated in API 24.

So you can use the latest android support library androidx.core.text.HtmlCompat. Before that, you need to include the dependency in your project.

`implementation 'androidx.core:core:1.0.1'`
  1. String html = "<u> 1.1 Underline using HtmlCompat.fromHtml()</u>"; //underline textview using HtmlCompat.fromHtml() method textview11.setText(HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY));

Using strings.xml,

  1. <string name="underline_text">1.3 &lt;u>Underline using HtmlCompat.fromHtml() and string resource&lt;/u></string>

textview13.setText(HtmlCompat.fromHtml(getString(R.string.underline_text), HtmlCompat.FROM_HTML_MODE_LEGACY));

using PaintFlags

  1. textview2.setPaintFlags(textview2.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); textview2.setText("2. Underline using setPaintFlags()");

using SpannableString

`String content1 = "3.1 Underline using SpannableString";
        SpannableString spannableString1 = new SpannableString(content1);
        spannableString1.setSpan(new UnderlineSpan(), 0, content1.length(), 0);
        textview31.setText(spannableString1);`

You can use Kotlin Extension and type your own drawUnderLine method.

fun TextView.drawUnderLine() {
    val text = SpannableString(this.text.toString())
    text.setSpan(UnderlineSpan(), 0, text.length, 0)
    this.text = text
}
yourTextView.drawUnderLine()

Its works for me.

tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

A simple and sustainable solution is to create a layer-list and make it as the background of your TextView:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="-5dp"
        android:right="-5dp"
        android:top="-5dp">
        <shape>
            <stroke
                android:width="1.5dp"
                android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>

For anyone still looking at this querstion. This is for a hyperlink but you can modify it for just a plain underline:

Create a drawable (hyperlink_underline.xml):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:top="-10dp"
        android:left="-10dp"
        android:right="-10dp">
    <shape android:shape="rectangle">
      <solid android:color="@android:color/transparent"/>

      <stroke android:width="2dp"
              android:color="#3498db"/>
    </shape>
  </item>
</layer-list>

Create a new style:

<style name="Hyperlink">
    <item name="android:textColor">#3498db</item>
    <item name="android:background">@drawable/hyperlink_underline</item>
  </style>

Then use this style on your TextView:

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    local:MvxBind="Text Id; Click ShowJobInfoCommand"
    style="@style/HyperLink"/>

just surround your text with < u > tag in your string.xml resource file

<string name="your_string"><u>Underlined text</u></string>

and in your Activity/Fragment

mTextView.setText(R.string.your_string);

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to textview

Kotlin: How to get and set a text to TextView in Android using Kotlin? How to set a ripple effect on textview or imageview on Android? The specified child already has a parent. You must call removeView() on the child's parent first (Android) Android: Creating a Circular TextView? android on Text Change Listener Android - Set text to TextView Placing a textview on top of imageview in android Rounded corner for textview in android Different font size of strings in the same TextView Auto-fit TextView for Android

Examples related to underline

Flutter: how to make a TextField with HintText but no Underline? How to Add a Dotted Underline Beneath HTML Text Changing Underline color Removing underline with href attribute How do you underline a text in Android XML? To draw an Underline below the TextView in Android CSS Box Shadow Bottom Only Get underlined text with Markdown Remove stubborn underline from link Underline text in UIlabel