Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set underline text on textview?

How to set underline text on textview?

I have used following code but it is not working.

tvHide.setText(Html.fromHtml("<p><u>Hide post</u></p>").toString());
like image 939
Nirav Modh Avatar asked Apr 13 '11 07:04

Nirav Modh


People also ask

How do you underline text in TextView?

You can also underline a portion of the text via code, it can be done by creating a SpannableString and then setting it as the TextView text property: SpannableString text = new SpannableString("Voglio sottolineare solo questa parola");text. setSpan(new UnderlineSpan(), 25, 6, 0);textView. setText(text);

How do you set an underline for text?

The quickest way to underline text is to press Ctrl+U and start typing. When you want to stop underlining, press Ctrl+U again. You can also underline text and spaces in several other ways.

How do you underline text in XML?

Just use <u> and <\u> in XML, and it's enough.


3 Answers

You have to use SpannableString for that :

String mystring=new String("Hello.....");
SpannableString content = new SpannableString(mystring);
content.setSpan(new UnderlineSpan(), 0, mystring.length(), 0);
yourtextview.setText(content);

Update : You can refer my answer on Underling TextView's here in all possible ways.

like image 179
Kartik Domadiya Avatar answered Oct 22 '22 06:10

Kartik Domadiya


Following are the some approaches for underlined text in android:

1st Approach

You can define your string in strings.xml

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

And use that string in your xml file

<TextView
            android:id="@+id/txt_underlined"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/your_string"/>

Or you can use that string in your Activity/Fragment

txtView.setText(R.string.your_string);

2nd Approach

To underline the text in TextView, you can use SpannableString

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

3rd Approach

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

txtView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
txtView.setText("Underlined Text");

4th Approach

Make use of Html.fromHtml(htmlString);

String htmlString="<u>Underlined Text</u>";
txtView.setText(Html.fromHtml(htmlString));

Or

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

Note:

If you have added this line android:textAllCaps="true" in your layout, then none of the above will work. For that, you have to define your string in Caps and then any of the above approach.

like image 30
akshay Avatar answered Oct 22 '22 06:10

akshay


Use this

tvHide.setPaintFlags(tvHide.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
like image 38
toxa_xa Avatar answered Oct 22 '22 08:10

toxa_xa