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.
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.
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.
The quickest way to underline text is to press Ctrl+U and start typing. When you want to stop underlining, press Ctrl+U again.
Try this code:
textview.setPaintFlags(textview.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
Here is the simplest way
TextView theTextView = new TextView(this); theTextView.setText(Html.fromHtml("<u>Text to underline</u>"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With