By Janusz

2010-03-07 02:26:35 8 Comments

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


@awsleiman 2015-09-04 08:41:53

For Button and TextView this is the easiest way:


Button button = (Button) findViewById(;
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);


TextView textView = (TextView) findViewById(;
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

@lynn8570 2019-02-20 09:42:14

<string name="set_target_with_underline">&lt;u>Set Target&lt;u> </string>

Note the Escape symbol in xml file

@Ray Lee 2019-01-29 11:16:00

The top voted answer is right and simplest. However, sometimes you may find that not working for some font, but working for others.(Which problem I just came across when dealing with Chinese.)

Solution is do not use "WRAP_CONTENT" only for your TextView, cause there is no extra space for drawing the line. You may set fixed height to your TextView, or use android:paddingVertical with WRAP_CONTENT.

@has19 2018-11-04 12:46:46

another solution is to a create a custom view that extend TextView as shown below

public class UnderLineTextView extends TextView {

    public UnderLineTextView(Context context) {

    public UnderLineTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);


and just add to xml as shown below

            android:text="underline text"

@Varun Raj 2019-08-12 11:09:34

Awesome!! But the color of the underline is gray how to change it to black or any other color?

@Killer 2018-08-16 11:36:34

In Kotlin extension function can be used. This can only be used from code, not xml.

fun TextView.underline() {
    paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG



@Fahmi Eshaq 2017-07-30 07:47:02

  1. Go to strings.xml resource file
  2. Add a string in the resource file with an HTML underline tag where necessary.

strings.xml HTML underline sample

  1. Call the string resource ID in your Java code as following:
  1. The output should have the word "Stackoverflow" underlined.

Furthermore, the following code will not print the underline:

String sampleString = getString(R.string.sample_string);

Instead, use the following code to retain rich text format:

CharSequence sampleString = getText(R.string.sample_string);

"You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string." Android documentation.

Refer to the documentation:

I hope this helps.

@Volodymyr Khodonovych 2016-06-30 08:59:44

The most recent approach of drawing underlined text is described by Romain Guy on medium with available source code on GitHub. This sample application exposes two possible implementations:

  • A Path-based implementation that requires API level 19
  • A Region-based implementation that requires API level 1

enter image description here

@Khemraj 2018-05-13 19:01:09

Damn, it is most simple to use

TextView tv = findViewById(;
tv.setText("some text");

Underline whole TextView

setUnderLineText(tv, tv.getText().toString());

Underline some part of TextView

setUnderLineText(tv, "some");

Also support TextView childs like EditText, Button, Checkbox

public void setUnderLineText(TextView tv, String textToUnderLine) {
        String tvt = tv.getText().toString();
        int ofe = tvt.indexOf(textToUnderLine, 0);

        UnderlineSpan underlineSpan = new UnderlineSpan();
        SpannableString wordToSpan = new SpannableString(tv.getText());
        for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(textToUnderLine, ofs);
            if (ofe == -1)
            else {
                wordToSpan.setSpan(underlineSpan, ofe, ofe + textToUnderLine.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);

check my answer here to make clickable underline text or underline multiple parts of TextView

@Samuel 2016-11-20 15:54:21

I used this xml drawable to create a bottom-border and applied the drawable as the background to my textview

<layer-list xmlns:android="">
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />

    <item android:top="-5dp" android:right="-5dp" android:left="-5dp">
            <solid android:color="@android:color/transparent" />
                    android:color="@color/pure_white" />

@Ivan Mitsura 2018-05-31 20:45:31

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

@Darush 2018-04-16 22:31:01

I simplified Samuel's answer:

<layer-list xmlns:android="">
                android:color="@color/colorAccent" />

@jk7 2018-03-14 00:49:33

A cleaner way instead of the
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); method is to use textView.getPaint().setUnderlineText(true);

And if you need to later turn off underlining for that view, such as in a reused view in a RecyclerView, textView.getPaint().setUnderlineText(false);

@Heisenberg 2017-08-23 12:15:25

Just use the attribute in string resource file e.g.

<string name="example"><u>Example</u></string>

@Devendra Anurag 2012-04-11 06:04:25

Strings.xml file content:

     <string name="my_text">This is an <u>underline</u>.</string> 

Layout xml file shold use the above string resource with below properties of textview, as shown below:



@Chris Rae 2013-01-23 22:04:34

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.

@Anthony Forloney 2010-03-07 02:29:57

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>.

    <string name="your_string_here">This is an <u>underline</u>.</string>

If you want to underline something from code use:

TextView textView = (TextView) view.findViewById(;
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

@jonney 2011-07-01 10:15:01

Hi, i tried the above resource xml code and it diddnt work. it continued to display the <u>underline</u> as a plain text

@Mark Renouf 2011-09-07 18:21:42

See also: android.text.Html.fromHtml( ) which returns a Spanned.

@gdrt 2014-04-01 18:13:05

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

@Giulio Piancastelli 2014-04-02 18:17:07

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 2014-05-16 13:18:41

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 2015-07-01 14:19:41

It won't show in editor, but when You start You app - it will be underlined.

@Alex 2016-06-26 04:24:23

what if I want to have a bit of space between text and underline ?

@fadden 2016-08-10 20:05:36

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 2018-01-21 13:19:11

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.

@hqzxzwb 2018-05-26 09:48:03

@jonney Replace < with &lt; and > with &gt;, then this should work.

@Kirill Vashilo 2017-05-05 11:26:05

check out the underscored clickable button style:

    style="@style/Widget.AppCompat.Button.Borderless.Colored" />


<string name="btn_add_contact"><u>Add new contact</u></string>


enter image description here

@xarlymg89 2018-03-22 12:16:39

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.

@user3786340 2017-03-31 12:35:50

A simple and flexible solution in xml:

  android:background="@color/your_color" />

@Ognyan 2012-03-31 10:22:00

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:

    <string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string>

Then in your code you can set the text with:

TextView textView = (TextView) view.findViewById(;
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));

@user898763452 2012-05-14 01:13:42

<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 2012-05-18 08:07:54

@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 2012-05-19 19:07:46

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 2012-05-22 14:08:22

@autotravis yes, you are correct. I use it with String.format(...). I've edited my answer, thank you for your feedback.

@Roy Solberg 2012-07-24 07:50:20

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().

@happyhardik 2012-09-14 10:36:28

<u>underlined</u> works for me in v2.1 :)

@gprathour 2015-03-17 05:46:26

One line solution

myTextView.setText(Html.fromHtml("<p><u>I am Underlined text</u></p>"));

It is bit late but could be useful for someone.

@Andrew.J 2014-08-04 11:11:38

try this code

in XML

 <string name="my_text"><![CDATA[This is an <u>underline</u>]]></string> 

in Code

TextView textView = (TextView) view.findViewById(;

Good Luck!

@Justin 2013-09-11 07:41:24

I know this is a late answer, but I came up with a solution that works pretty well... I took the answer from Anthony Forloney for underlining text in code and created a subclass of TextView that handles that for you. Then you can just use the subclass in XML whenever you want to have an underlined TextView.

Here is the class I created:

import android.content.Context;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.TextView;

 * Created with IntelliJ IDEA.
 * User: Justin
 * Date: 9/11/13
 * Time: 1:10 AM
public class UnderlineTextView extends TextView
    private boolean m_modifyingText = false;

    public UnderlineTextView(Context context)

    public UnderlineTextView(Context context, AttributeSet attrs)
        super(context, attrs);

    public UnderlineTextView(Context context, AttributeSet attrs, int defStyle)
        super(context, attrs, defStyle);

    private void init()
        addTextChangedListener(new TextWatcher()
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
                //Do nothing here... we don't care

            public void onTextChanged(CharSequence s, int start, int before, int count)
                //Do nothing here... we don't care

            public void afterTextChanged(Editable s)
                if (m_modifyingText)



    private void underlineText()
        if (m_modifyingText)

        m_modifyingText = true;

        SpannableString content = new SpannableString(getText());
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

        m_modifyingText = false;

Now... whenever you want to create an underlined textview in XML, you just do the following:

    android:text="This text is underlined"

I have added additional options in this XML snippet to show that my example works with changing the text color, size, and style...

Hope this helps!

@Justin 2014-03-14 20:18:21

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.

@Rick Pastoor 2013-07-12 09:00:31

I had a problem where I'm using a custom font and the underline created with the resource file trick (<u>Underlined text</u>) did work but Android managed to transform the underline to a sort of strike trough.

I used this answer to draw a border below the textview myself: Obviously this doesn't work for partial underlined text or multilined text.

@vado 2012-06-08 10:44:58

You can try with

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);

@kwishnu 2013-07-27 05:43:25

Maybe a minor point, but OP wanted this defined in XML layout file; otherwise, this is a great solution.

@Bobs 2013-08-17 09:19:52

use this solution if you want to clear it

@Rudy Kurniawan 2016-05-16 09:19:04

this solution can be used for a Button too

@Martin Marconcini 2018-02-23 20:53:38

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.

@Albert Vila Calvo 2019-09-06 12:01:11

In Kotlin: textView.paintFlags = textView.paintFlags or Paint.UNDERLINE_TEXT_FLAG

Related Questions

Sponsored Content

51 Answered Questions

[SOLVED] Stop EditText from gaining focus at Activity startup

97 Answered Questions

55 Answered Questions

[SOLVED] How do I fix 'android.os.NetworkOnMainThreadException'?

30 Answered Questions

17 Answered Questions

[SOLVED] "Debug certificate expired" error in Eclipse Android plugins

64 Answered Questions

[SOLVED] How do I center text horizontally and vertically in a TextView?

  • 2009-01-11 00:27:55
  • pupeno
  • 1180214 View
  • 1927 Score
  • 64 Answer
  • Tags:   android textview

77 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

18 Answered Questions

[SOLVED] Can the Android Layout folder contain subfolders?

7 Answered Questions

[SOLVED] What's "tools:context" in Android layout files?

Sponsored Content