How do you underline a text in Android XML?

Go To StackoverFlow.com

86

How do you underline a text in an XML file? I can't find an option in textStyle.

2012-04-04 20:47
by Michael Zeuner
u have try using style="text-decoration: underline;"ρяσѕρєя K 2012-04-04 20:53
could you should in full how to use that - Michael Zeuner 2012-04-04 20:56
u have try this textView.setText(Html.fromHtml("<u>"+"testest"+"</u>"));ρяσѕρєя K 2012-04-04 21:01


143

If you are using a string resource xml file (supports HTML tags), it can be done using<b> </b>, <i> </i> and <u> </u>.

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

If you want to underline something from code use:

TextView tv = (TextView) view.findViewById(R.id.tv);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content);

Hope this helps

2012-04-04 20:55
by George Artemiou
I tryed this. the string does not come up underlined - Michael Zeuner 2012-04-04 20:57
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 indeed never failed on me, so I would recommend it as the most reliable solution - Giulio Piancastelli 2014-04-02 18:06
You don't need to set text in Java. Just use <u> and <\u> in XML, and it's enough - Maksim Dmitriev 2014-05-06 11:49
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 - Alvin 2015-04-15 11:38
the fact that you have to convert a string to HTML when Google already gives us bold/italic/normal is a pretty slack considering we've had Android for over 5 years...angryITguy 2016-12-15 06:30
I'm getting the underline starting in the space before the word... <string name="not_registered">Not Registered? Click <u>here</u> to register!"</string> Anyone know why? The underline is starting in the space immediately after the "k" in "Click" - Kevin Bright 2018-10-26 01:16


51

Use this:

TextView txt = (TextView) findViewById(R.id.Textview1);
txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
2013-12-27 05:13
by abh22ishek
Be aware that this solution always underlines the whole text, so it's not feasible if one wants to underline only a portion of it. For that, you need UnderlineSpan - Giulio Piancastelli 2014-04-02 18:08
This is the most elegant answer for underlining the whole textvie - voghDev 2016-11-07 09:49


19

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

If it does not work then

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

Because "<" could be a keyword at some time.

And for Displaying

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));
2012-04-05 07:06
by Bhavin
Works a treat, thank - Donal Rafferty 2013-01-07 12:37
This works...but if for some reason you set the TextView to use the xml attribute android:textAllCaps="true", the underline will not appear. Remove this modifier and the underline will show as intended. Just a heads up : - Matt W 2013-07-24 03:25


3

First of all go to String.xml file

you can add any html attributes like , italic or bold or underline here.

    <resource>
        <string name="your_string_here">This is an <u>underline</u>.</string>
    </resources>
2014-04-21 11:28
by Xar E Ahmer
This was already answered above - Shygar 2014-05-27 01:43


2

Another way to do it, is creating a custom component extending TextView. It's good for cases where you need to have multiple underlined TextViews.

Here's the code for the component:

package com.myapp.components;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;

public class LinkTextView extends AppCompatTextView {
    public LinkTextView(Context context) {
        this(context, null);
    }

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

    @Override
    public void setText(CharSequence text, BufferType type) {
        SpannableString content = new SpannableString(text);
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

        super.setText(content, type);
    }
}

And how to use it in xml:

<com.myapp.components.LinkTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
2018-01-11 12:44
by Tharkius


1

I used below method, it's work for me. below is example for Button but we can use in TextView as well.

Button btnClickMe= (Button) findViewById(R.id.btn_click_me);
btnClickMe.setPaintFlags(btnClickMe.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
2016-11-07 06:28
by Hitesh Dhamshaniya


1

You can use the markup below, but note that if you set the textAllCaps to true the underline effect would be removed.

<resource>
    <string name="my_string_value">I am <u>underlined</u>.</string>
</resources>


Note

Using textAllCaps with a string (login_change_settings) that contains markup; the markup will be dropped by the caps conversion

The textAllCaps text transform will end up calling toString on the CharSequence, which has the net effect of removing any markup such as . This check looks for usages of strings containing markup that also specify textAllCaps=true.

2017-11-04 12:44
by Tebo


0

To complete Bhavin answer. For exemple, to add underline or redirection.

((TextView) findViewById(R.id.tv_cgu)).setText(Html.fromHtml(getString(R.string.upload_poi_CGU)));

<string name="upload_poi_CGU"><![CDATA[ J\'accepte les <a href="">conditions générales</a>]]></string>

and you can know compatible tag here : http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

2014-06-11 14:29
by Anthone


0

There are different ways to achieve underlined text in an Android TextView.

1.<u>This is my underlined text</u> or

I just want to underline <u>this</u> word

2.You can do the same programmatically.

`textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);`

3.It can be done by creating a SpannableString and then setting it as the TextView text property

SpannableString text = new SpannableString("Voglio sottolineare solo questa parola");
text.setSpan(new UnderlineSpan(), 25, 6, 0);
textView.setText(text);
2018-08-27 07:07
by Anandharaj R
Ads