131

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?

Mxyk
  • 10,678
  • 16
  • 57
  • 76
David Brown
  • 4,783
  • 17
  • 50
  • 75

10 Answers10

346

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"));
akshay
  • 5,811
  • 5
  • 39
  • 58
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
  • 1
    Glad to hear that..Mark this question as solved so that other users can refer it. – Kartik Domadiya Nov 07 '11 at 06:37
  • 4
    A third approach would be using `Html.fromHtml("This text will be underlined")`, but I have to admit I'm a much bigger fan of using SpannableStrings. @Kartik: You might as well use a `StrikethroughSpan` on the text to create the strikethrough effect. :) – MH. Nov 07 '11 at 07:14
  • Love the 3rd approach. Short, simple and concise and adds just one extra line of code to my program. – Matt Feb 15 '13 at 15:20
  • I know this is kinda old, but is there a way to set the thickness of the underline? – Andreas Wong Jun 03 '13 at 14:40
  • which of those is used for the preferenceActivity content , in the PreferenceCategory ? – android developer Jul 01 '13 at 08:21
  • what is the difference between all in terms of performance? I needed it for something which is not html. – Ankit Mar 21 '16 at 14:57
50

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);
Sarpe
  • 5,747
  • 2
  • 28
  • 26
  • 7
    @CHAKRAVARTHI it woun't work if you cast the text to string before setting. E.g. `textView.setText(getString(R.string.text))` **<- wrong**. **Right:** `textView.setText(getText(R.string.text))` or just `textView.setText(R.string.text)`. The reason beind this that `getText()` returns `Spannable` with underline spans, but if you use `getString()` it will convert the `Spannable` to `String` resulting spans being removed. – Yaroslav Mytkalyk Nov 10 '14 at 10:01
  • good point! I've edited my current answer in order to be more clear – Sarpe Nov 24 '14 at 18:59
  • Thanks for sharing.... Very convenient way ! – Shafiq ur Rehman Jul 05 '22 at 14:24
21

Its works for me.

tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
Mani
  • 3,394
  • 3
  • 30
  • 36
13

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"/>
Daniel
  • 270
  • 3
  • 14
  • thanks, this is very useful, and I learn Something new, and with top left etc, attribute we can use as upper or side line. thanks again – Noor Hossain Jan 02 '21 at 12:43
5

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>
Darush
  • 11,403
  • 9
  • 62
  • 60
3

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>
Valeriya
  • 1,067
  • 2
  • 16
  • 31
3

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);`
Vijay Ram
  • 385
  • 5
  • 12
1

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()
Amjad Alwareh
  • 2,926
  • 22
  • 27
1

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"
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
0

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
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19