Rectangle 27 0

To draw an Underline below the TextView in Android?


<string name="your_string"><u>Underlined text</u></string>
Spannable
String
getString()
getText()
mTextView.setText(R.string.your_string);
textView.setText(R.string.text)
textView.setText(getString(R.string.text))
textView.setText(getText(R.string.text))

It does not work.

and in your Activity/Fragment

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

Note
Rectangle 27 0

To draw an Underline below the TextView in Android?


Html.fromHtml(htmlString);
String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));
String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);
mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");
txtView.setText(Html.fromHtml("<u>underlined</u> text"));

A third approach would be using Html.fromHtml("<u>This text will be underlined</u>"), 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. :)

For underling the text in TextView you have to use SpannableString

Glad to hear that..Mark this question as solved so that other users can refer it.

I know this is kinda old, but is there a way to set the thickness of the underline?

Let me explain you all approaches :

Love the 3rd approach. Short, simple and concise and adds just one extra line of code to my program.

There are three ways of underling the text in TextView.

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

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

Note
Rectangle 27 0

To draw an Underline below the TextView in Android?


Html.fromHtml(htmlString);
String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));
String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);
mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");
txtView.setText(Html.fromHtml("<u>underlined</u> text"));

A third approach would be using Html.fromHtml("<u>This text will be underlined</u>"), 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. :)

For underling the text in TextView you have to use SpannableString

Glad to hear that..Mark this question as solved so that other users can refer it.

I know this is kinda old, but is there a way to set the thickness of the underline?

Let me explain you all approaches :

Love the 3rd approach. Short, simple and concise and adds just one extra line of code to my program.

There are three ways of underling the text in TextView.

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

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

Note
Rectangle 27 0

To draw an Underline below the TextView in Android?


<string name="your_string"><u>Underlined text</u></string>
Spannable
String
getString()
getText()
mTextView.setText(R.string.your_string);
textView.setText(R.string.text)
textView.setText(getString(R.string.text))
textView.setText(getText(R.string.text))

It does not work.

and in your Activity/Fragment

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

Note
Rectangle 27 0

To draw an Underline below the TextView in Android?


<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    local:MvxBind="Text Id; Click ShowJobInfoCommand"
    style="@style/HyperLink"/>
<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>
<style name="Hyperlink">
    <item name="android:textColor">#3498db</item>
    <item name="android:background">@drawable/hyperlink_underline</item>
  </style>

Create a drawable (hyperlink_underline.xml):

Create a new style:

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

Note
Rectangle 27 0

To draw an Underline below the TextView in Android?


<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    local:MvxBind="Text Id; Click ShowJobInfoCommand"
    style="@style/HyperLink"/>
<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>
<style name="Hyperlink">
    <item name="android:textColor">#3498db</item>
    <item name="android:background">@drawable/hyperlink_underline</item>
  </style>

Create a drawable (hyperlink_underline.xml):

Create a new style:

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

Note
Rectangle 27 0

To draw an Underline below the TextView in Android?


tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
Note
Rectangle 27 0

To draw an Underline below the TextView in Android?


tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
Note