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

- 33,559
- 24
- 104
- 119

- 1,736
- 3
- 15
- 23
-
u have try using `style="text-decoration: underline;"` – ρяσѕρєя K Apr 04 '12 at 20:53
-
1could you should in full how to use that? – Michael Zeuner Apr 04 '12 at 20:56
-
u have try this `textView.setText(Html.fromHtml(""+"testest"+""));` – ρяσѕρєя K Apr 04 '12 at 21:01
11 Answers
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);

- 20,030
- 7
- 43
- 238

- 3,076
- 2
- 19
- 36
-
12
-
6I have come across cases when underlying through `` 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 Apr 02 '14 at 18:06
-
You don't need to set text in Java. Just use `` and `<\u>` in XML, and it's enough. – Maksim Dmitriev May 06 '14 at 11:49
-
11in 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 Apr 15 '15 at 11:38
-
1the 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 Dec 15 '16 at 06:30
-
I'm getting the underline starting in the space before the word... `
Not Registered? Click here to register!" ` Anyone know why? The underline is starting in the space immediately after the "k" in "Click". – Kevin Bright Oct 26 '18 at 01:16
Use this:
TextView txt = (TextView) findViewById(R.id.Textview1);
txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

- 15,368
- 5
- 42
- 62

- 2,631
- 4
- 27
- 47
-
1Be 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 Apr 02 '14 at 18:08
-
Need help how to write the second line of this answer in Kotlin? It throws compile time error at the | character. I'm not aware of the syntax of including this character. – Dhananjay M Mar 14 '22 at 05:43
<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 <u>underline</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)));

- 6,020
- 4
- 24
- 26
-
3This 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 Jul 24 '13 at 03:25
First of all, go to String.xml file
you can add any HTML attributes like , italic or bold or underline here.
<resources>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>

- 23
- 7

- 33,936
- 20
- 234
- 300
I used below method, it worked 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);

- 8,084
- 8
- 48
- 62

- 2,088
- 2
- 16
- 23
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!" />

- 2,744
- 2
- 20
- 31
-
1I would strongly suggest to use this approach compared to the answers using string resources. My main concern is that string resources combined with styling mixes design with content. If your application supports multiple languages you'll have to include the styling in every internationalization. Very easy accidentally delete styling when copy pasting the translations. – Omnibyte Jun 17 '22 at 11:51
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.

- 14,981
- 11
- 56
- 70
Create a String resource:
<string name="HEADER_DELTA"><b><u>DELTA</u></b></string>
and add to your Textview
<TextView
android:id="@+id/txtDeltaText"
style="@style/Default_TextBox_Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center_horizontal"
android:text="@string/HEADER_DELTA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/txtActualMetric"
app:layout_constraintTop_toBottomOf="@+id/txtMetricName" />

- 276
- 2
- 5
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

- 2,156
- 21
- 26
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);

- 169
- 1
- 6
If you want to compare text String or the text will change dynamically then you can created a view in Constraint layout it will adjust according to text length like this
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txt_Previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="Last Month Rankings"
android:textColor="@color/colorBlue"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="0.7dp"
android:background="@color/colorBlue"
app:layout_constraintEnd_toEndOf="@+id/txt_Previous"
app:layout_constraintStart_toStartOf="@+id/txt_Previous"
app:layout_constraintBottom_toBottomOf="@id/txt_Previous"/>
</android.support.constraint.ConstraintLayout>

- 145
- 1
- 12
-
2I have no idea how this answer relates to the question. OP didn't mention comparing texts at all and there's nothing in your answer about underlines. – Omnibyte Jun 17 '22 at 11:46