Rectangle 27 1

Can I underline text in an android layout?


<resources>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);

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

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.

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

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

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

You 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

Note
Rectangle 27 1

Can I underline text in an android layout?


String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));
String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);//where first 0 shows the starting and udata.length() shows the ending span.if you want to span only part of it than you can change these values like 5,8 then it will underline part of it.
mTextView.setText(content);
mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

Three ways of underling the text in TextView

For underling the text in TextView you have to use SpannableString

Loved the 2nd approach, neat and clean

The word is SpannableString not what you said Pedro Varela

You can make use of setPaintFlags method of TextView to underline the text of TextView.

You can refer constants of Paint class if you want to strike thru the text.

Note
Rectangle 27 1

Can I underline text in an android layout?


<resource>
    <string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string>
</resources>
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));
textView.setText(Html.fromHtml(String.format(getString(...), ...)))

<u>underlined here</u> works perfectly. u>underline/u> did not work. This is for Android 2.2. Maybe different versions interpret it differently.

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

As stated in the documentations you must escape (html entity encoded) opening bracket of the inner tags with , e.g. result should look like:

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

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

Then in your code you can set the text with:

Note
Rectangle 27 1

Can I underline text in an android layout?


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

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

You can try with

amazing :) simple and perfect :)

this solution can be used for a Button too

use this solution if you want to clear it

Note
Rectangle 27 1

Can I underline text in an android layout?


<com.your.package.name.UnderlineTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:text="This text is underlined"
    android:textColor="@color/blue_light"
    android:textSize="12sp"
    android:textStyle="italic"/>
import android.content.Context;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.UnderlineSpan;
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)
    {
        super(context);
        init();
    }

    public UnderlineTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

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

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

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

            @Override
            public void afterTextChanged(Editable s)
            {
                if (m_modifyingText)
                    return;

                underlineText();
            }
        });

        underlineText();
    }

    private void underlineText()
    {
        if (m_modifyingText)
            return;

        m_modifyingText = true;

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

        m_modifyingText = false;
    }
}

Here is the class I created:

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

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.

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

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.

Note
Rectangle 27 1

Can I underline text in an android layout?


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

Can I underline text in an android layout?


<TextView android:layout_width="fill_parent"
     android:gravity="center_horizontal"
          android:layout_height="wrap_content"
     android:selectAllOnFocus="false"
          android:linksClickable="false"
     android:autoLink="all"
          android:text="@string/my_text"
/>
<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:

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.

Note
Rectangle 27 1

Can I underline text in an android layout?


<resources>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);

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

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.

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

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

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

You 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

Note
Rectangle 27 1

Can I underline text in an android layout?


String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));
String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);//where first 0 shows the starting and udata.length() shows the ending span.if you want to span only part of it than you can change these values like 5,8 then it will underline part of it.
mTextView.setText(content);
mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

Three ways of underling the text in TextView

For underling the text in TextView you have to use SpannableString

Loved the 2nd approach, neat and clean

The word is SpannableString not what you said Pedro Varela

You can make use of setPaintFlags method of TextView to underline the text of TextView.

You can refer constants of Paint class if you want to strike thru the text.

Note
Rectangle 27 1

Can I underline text in an android layout?


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

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

You can try with

amazing :) simple and perfect :)

this solution can be used for a Button too

use this solution if you want to clear it

Note