Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set underline text to TextView in android programmatically

I want to know how to set underline text to TextView in android? Please make a note that I don't have capability to set a strings.xml with pre-populated strings is there any way to add it with pure java code in Android.

like image 360
Pratik Patel Avatar asked Oct 20 '12 14:10

Pratik Patel


People also ask

How to underline TextView in XML Android Studio?

Just use <u> and <\u> in XML, and it's enough. in my case, android studio preview was not able to determine the html tag. but once i ran the project in real device, underlined text shown happily.

How to add line Under text in Android?

Go to res > drawable > new > drawable resource file and create a new file and name it “dashed_underline. xml” and define all properties of the dashed line that we need. Later we will add this as the background of TextView. Below is the code for dashed_underline.

How do you underline in a 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.


2 Answers

Try this code:

   textview.setPaintFlags(textview.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG); 
like image 92
yahya.can Avatar answered Oct 19 '22 22:10

yahya.can


Here is the simplest way

TextView theTextView = new TextView(this);  theTextView.setText(Html.fromHtml("<u>Text to underline</u>")); 
like image 34
Vishal Pawar Avatar answered Oct 19 '22 22:10

Vishal Pawar