To draw an Underline below the TextView in Android

AndroidTextviewUnderline

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

There are three ways of underling the text in TextView.

  1. SpannableString

  2. setPaintFlags(); of TextView

  3. Html.fromHtml();

Let me explain you all approaches :

1st Approach

For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

2nd Approach

You can make use of setPaintFlags method of TextView to underline the text of TextView.

For eg.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

You can refer constants of Paint class if you want to strike thru the text.

3rd Approach

Make use of Html.fromHtml(htmlString);

String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));

OR

txtView.setText(Html.fromHtml("<u>underlined</u> text"));

Solution 2 - Android

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);

Solution 3 - Android

Its works for me.

tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

Solution 4 - Android

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"/>

Solution 5 - Android

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>

Solution 6 - Android

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>

Solution 7 - Android

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'`

2. 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);`

Solution 8 - Android

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()

Solution 9 - Android

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

Solution 10 - Android

If you want to under line Email Address Text and want to open Email related app when user click on that particular Email address then here is best way. Simply use below property.

android:autoLink="email"

Similarly you can you for website

android:autoLink="web"

Additionally if you want to set text colour you can you below property

android:textColorLink="@color/email_text_color"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid BrownView Question on Stackoverflow
Solution 1 - AndroidKartik DomadiyaView Answer on Stackoverflow
Solution 2 - AndroidSarpeView Answer on Stackoverflow
Solution 3 - AndroidmaniView Answer on Stackoverflow
Solution 4 - AndroidDanielView Answer on Stackoverflow
Solution 5 - AndroidDarushView Answer on Stackoverflow
Solution 6 - AndroidValeriyaView Answer on Stackoverflow
Solution 7 - AndroidVijay RamView Answer on Stackoverflow
Solution 8 - AndroidAmjad AlwarehView Answer on Stackoverflow
Solution 9 - AndroidYeldar NurpeissovView Answer on Stackoverflow
Solution 10 - AndroidMayur DabhiView Answer on Stackoverflow