Can I underline text in an Android layout?

620,127

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 &lt;, e.g. result should look like:

<resource>
    <string name="your_string_here">This is an &lt;u&gt;underline&lt;/u&gt;.</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);
Share:
620,127
SHRISH M
Author by

SHRISH M

Updated on December 15, 2021

Comments

  • SHRISH M
    SHRISH M over 2 years

    How can I define underlined text in an Android layout xml file?

  • Jono
    Jono almost 13 years
    Hi, i tried the above resource xml code and it diddnt work. it continued to display the <u>underline</u> as a plain text
  • Josh
    Josh almost 13 years
    See also: android.text.Html.fromHtml( ) which returns a Spanned.
  • user898763452
    user898763452 about 12 years
    <u>underlined here</u> works perfectly. &lt;u>underline&lt;/u> did not work. This is for Android 2.2. Maybe different versions interpret it differently.
  • Ognyan
    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
    user898763452 about 12 years
    I've tested <u>underlined</u> on android 2.3 and it works. &lt;u>underline&lt;/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
    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
    Roy Solberg almost 12 years
    On 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
    Chris Rae over 11 years
    Make 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
    kwishnu almost 11 years
    Maybe a minor point, but OP wanted this defined in XML layout file; otherwise, this is a great solution.
  • Bobs
    Bobs almost 11 years
    use this solution if you want to clear it
  • Justin
    Justin over 10 years
    While 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
    gdrt about 10 years
    You should type it in strings.xml file itself, not doing it by add button. Otherwise you will get this &lt;u&gt; in your strings.xml file and <u>underline</u> in your code
  • Bob Barbara
    Bob Barbara about 10 years
    I 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 by UnderlineSpan has yet to fail on me, so I would recommend it as the most reliable solution.
  • Nilesh
    Nilesh about 10 years
    Writing 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
    jean d'arme almost 9 years
    It won't show in editor, but when You start You app - it will be underlined.
  • Rudy Kurniawan
    Rudy Kurniawan about 8 years
    this solution can be used for a Button too
  • Alex
    Alex almost 8 years
    what if I want to have a bit of space between text and underline ?
  • fadden
    fadden almost 8 years
    FWIW, <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
    Gal Rom over 6 years
    Don'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
    Martin Marconcini over 6 years
    Careful 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
    xarlymg89 over 6 years
    The 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
    hqzxzwb about 6 years
    @jonney Replace < with &lt; and > with &gt;, then this should work.
  • Ivan Mitsura
    Ivan Mitsura about 6 years
    this 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
    OhhhThatVarun almost 5 years
    Awesome!! But the color of the underline is gray how to change it to black or any other color?
  • Albert Vila Calvo
    Albert Vila Calvo almost 5 years
    In Kotlin: textView.paintFlags = textView.paintFlags or Paint.UNDERLINE_TEXT_FLAG
  • Tuan Chau
    Tuan Chau over 4 years
    For String resource, we need to wrap the text inside <![CDATA[ content ]] -> <string ...><![CDATA[content]]</string>
  • Mark Delphi
    Mark Delphi over 4 years
    You saved my time as well as number of code lines :)
  • Jeffrey Blattman
    Jeffrey Blattman almost 4 years
    For this, this forces the text to be upper case, gives it a large border, and makes it larger than the default text.
  • Edric
    Edric almost 4 years
    How is this answer any different from this answer posted on April 5?
  • StayCool
    StayCool over 3 years
    and how to remove that UNDERLINE_TEXT_FLAG?)