Can I underline text in an Android layout?
Solution 1
It can be achieved if you are using a string resource xml file, which supports HTML tags like <b></b>
, <i></i>
and <u></u>
.
<resources>
<string name="your_string_here"><![CDATA[This is an <u>underline</u>.]]></string>
</resources>
If you want to underline something from code use:
TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
Solution 2
You can try with
textview.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Solution 3
The "accepted" answer above does NOT work (when you try to use the string like textView.setText(Html.fromHtml(String.format(getString(...), ...)))
.
As stated in the documentations you must escape (html entity encoded) opening bracket of the inner tags with <
, e.g. result should look like:
<resource>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
Then in your code you can set the text with:
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));
Solution 4
Strings.xml file content:
<resource>
<string name="my_text">This is an <u>underline</u>.</string>
</resources>
Layout xml file shold use the above string resource with below properties of textview, as shown below:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/my_text"
android:selectAllOnFocus="false"
android:linksClickable="false"
android:autoLink="all"
/>
Solution 5
For Button and TextView this is the easiest way:
Button:
Button button = (Button) findViewById(R.id.btton1);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Textview:
TextView textView = (TextView) findViewById(R.id.textview1);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
SHRISH M
Updated on December 15, 2021Comments
-
SHRISH M over 2 years
How can I define underlined text in an Android layout
xml
file? -
Jono almost 13 yearsHi, i tried the above resource xml code and it diddnt work. it continued to display the <u>underline</u> as a plain text
-
Josh almost 13 yearsSee also: android.text.Html.fromHtml( ) which returns a Spanned.
-
user898763452 about 12 years<u>underlined here</u> works perfectly. <u>underline</u> did not work. This is for Android 2.2. Maybe different versions interpret it differently.
-
Ognyan about 12 years@autotravis which one is for 2.2? I did not tested it on older versions and it will be quite unfortunate if different versions handle it differently... Also at least current documentation states that it have to be escaped (link is in the answer).
-
user898763452 about 12 yearsI've tested <u>underlined</u> on android 2.3 and it works. <u>underline</u> does not work for 2.3. The docs say "Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place." So I guess you would use the escaping if you run it through that String.format(...) method.
-
Ognyan about 12 years@autotravis yes, you are correct. I use it with String.format(...). I've edited my answer, thank you for your feedback.
-
Roy Solberg almost 12 yearsOn 2.3 and 4.1 (only ones I tried so far) you can just use textView.setText(getText(R.string.text)) instead of having to use getString(), Html.fromHtml() and String.format().
-
Chris Rae over 11 yearsMake sure you edit the string resource file inside the actual XML rather than inside the helper editing UI in Eclipse, at it will escape the <s.
-
kwishnu almost 11 yearsMaybe a minor point, but OP wanted this defined in XML layout file; otherwise, this is a great solution.
-
Bobs almost 11 yearsuse this solution if you want to clear it
-
Justin over 10 yearsWhile this example works, I quickly realized that if you ever want to have additional control in XML that this just won't do... I have switched to a better solution that involves the following steps: 1) Subclass textview 2) Add support to underline the text via custom attributes to do the underlining as specified above. The only difference is that you only execute the underline code if the custom attribute is set.
-
gdrt about 10 yearsYou should type it in strings.xml file itself, not doing it by add button. Otherwise you will get this <u> in your strings.xml file and <u>underline</u> in your code
-
Bob Barbara about 10 yearsI have come across cases when underlying through
<u>
tags does not work, e.g. sometimes if you are using a custom font. However, underlying programmatically byUnderlineSpan
has yet to fail on me, so I would recommend it as the most reliable solution. -
Nilesh about 10 yearsWriting in the xml resource file did not work for me on Android SDK 4.0.3 . But Java code worked. Thank you
-
jean d'arme almost 9 yearsIt won't show in editor, but when You start You app - it will be underlined.
-
Rudy Kurniawan about 8 yearsthis solution can be used for a Button too
-
Alex almost 8 yearswhat if I want to have a bit of space between text and underline ?
-
fadden almost 8 yearsFWIW,
<u>text</u>
worked for a string resource in an app running on Marshmallow. I entered it directly with a text editor, not a fancy text-escaping resource editor. -
Gal Rom over 6 yearsDon't use getString to fetch the string from your xml, use it directly e.g: tv.setText(R.string.your_html_string); Then it will work.
-
Martin Marconcini over 6 yearsCareful if you use custom fonts or similar, you may need to
| Paint.ANTI_ALIAS_FLAG
as well or you text will look very sharp. This manifests in lower APIs more often than not. -
xarlymg89 over 6 yearsThe look and feel of Widget.AppCompat.Button.Borderless.Colored is much better than underlining, however, as @Kiril Vashilo described, you can do it anyway.
-
hqzxzwb about 6 years@jonney Replace
<
with<
and>
with>
, then this should work. -
Ivan Mitsura about 6 yearsthis is the perfect solution for all view underline with custom color. do not disrespect the transparent background here, for android 4 it is required, your view will have black background w/o it
-
OhhhThatVarun almost 5 yearsAwesome!! But the color of the underline is gray how to change it to black or any other color?
-
Albert Vila Calvo almost 5 yearsIn Kotlin:
textView.paintFlags = textView.paintFlags or Paint.UNDERLINE_TEXT_FLAG
-
Tuan Chau over 4 yearsFor String resource, we need to wrap the text inside
<![CDATA[
content]]
-><string ...><![CDATA[content]]</string>
-
Mark Delphi over 4 yearsYou saved my time as well as number of code lines :)
-
Jeffrey Blattman almost 4 yearsFor this, this forces the text to be upper case, gives it a large border, and makes it larger than the default text.
-
Edric almost 4 yearsHow is this answer any different from this answer posted on April 5?
-
StayCool over 3 yearsand how to remove that UNDERLINE_TEXT_FLAG?)