Can I underline text in an Android layout?

AndroidAndroid LayoutFonts

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

You can try with

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

Solution 3 - Android

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

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

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);

Solution 6 - Android

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
}

Usage:

 tv_change_number.underline()
 tv_resend_otp.underline()

Solution 7 - Android

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.

Solution 8 - Android

To do that in Kotlin:

yourTextView.paint?.isUnderlineText = true

Solution 9 - Android

check out the underscored clickable button style:

<TextView
    android:id="@+id/btn_some_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_add_contact"
    android:textAllCaps="false"
    android:textColor="#57a0d4"
    style="@style/Widget.AppCompat.Button.Borderless.Colored" />

strings.xml:

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

Result:

enter image description here

Solution 10 - Android

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);

Solution 11 - Android

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

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

<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"/>

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!

Solution 12 - Android

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

Solution 13 - Android

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    
    <item android:top="-5dp" android:right="-5dp" android:left="-5dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                    android:width="1.5dp"
                    android:color="@color/pure_white" />
        </shape>
    </item>
</layer-list>

Solution 14 - Android

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

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

Solution 15 - Android

If you want to achieve this in XML, declare your string in resource and put that resource value into underline tag (<u></u>) of HTML. in TextView, add

android:text="@string/your_text_reference"

And in string resource value,

<string name="your_text_reference"><u>Underline me</u></string>

If you want to achieve this programmatically, for Kotlin use

textView.paintFlags = textView.paintFlags or Paint.UNDERLINE_TEXT_FLAG

or,

textView.text = Html.fromHtml("<p><u>Underline me</u></p>")

Solution 16 - Android

A simple and flexible solution in xml:

<View
  android:layout_width="match_parent"
  android:layout_height="3sp"
  android:layout_alignLeft="@+id/your_text_view_need_underline"
  android:layout_alignRight="@+id/your_text_view_need_underline"
  android:layout_below="@+id/your_text_view_need_underline"
  android:background="@color/your_color" />

Solution 17 - Android

Most Easy Way

TextView tv = findViewById(R.id.tv);
tv.setText("some text");
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)
                break;
            else {
                wordToSpan.setSpan(underlineSpan, ofe, ofe + textToUnderLine.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            }
        }
    }

If you want

- Clickable underline text?

- Underline multiple parts of TextView?

Then Check This Answer

Solution 18 - Android

I simplified Samuel's answer:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--https://stackoverflow.com/a/40706098/4726718-->
    <item
        android:left="-5dp"
        android:right="-5dp"
        android:top="-5dp">
        <shape>
            <stroke
                android:width="1.5dp"
                android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>

Solution 19 - Android

try this code

in XML

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

in Code

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.my_text)));

Good Luck!

Solution 20 - Android

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

public class UnderLineTextView extends TextView {

    public UnderLineTextView(Context context) {
        super(context);
        this.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    public UnderLineTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

}

and just add to xml as shown below

<yourpackage.UnderLineTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="underline text"
 />

Solution 21 - Android

Very compact, kotlin version:

tvTitle.apply { 
    text = "foobar"
    paint?.isUnderlineText = true
}

Solution 22 - Android

try with Class

for java

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

for Kotlin

textview.setPaintFlags(textview.getPaintFlags() or Paint.UNDERLINE_TEXT_FLAG)

Solution 23 - Android

  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:

> sampleTextView.setText(R.string.sample_string);

  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);
sampleTextView.setText(sampleString);

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

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

"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: https://developer.android.com/guide/topics/resources/string-resource.html

I hope this helps.

Solution 24 - Android

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.

Solution 25 - Android

HtmlCompat.fromHtml(
                    String.format(context.getString(R.string.set_target_with_underline)),
                    HtmlCompat.FROM_HTML_MODE_LEGACY)
<string name="set_target_with_underline">&lt;u>Set Target&lt;u> </string>

Note the Escape symbol in xml file

Solution 26 - Android

Its quite late to answer this but suppose if anyone wants to get the text dynamically then they can use this simple one line in their java code which works:

 textView.setText(Html.fromHtml("<p><u>" + get_name + "</u></p>"));

Solution 27 - Android

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: https://stackoverflow.com/a/10732993/664449. Obviously this doesn't work for partial underlined text or multilined text.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJanuszView Question on Stackoverflow
Solution 1 - AndroidAnthony ForloneyView Answer on Stackoverflow
Solution 2 - AndroidvadoView Answer on Stackoverflow
Solution 3 - AndroidOgnyanView Answer on Stackoverflow
Solution 4 - AndroidDevendra AnuragView Answer on Stackoverflow
Solution 5 - AndroidawsleimanView Answer on Stackoverflow
Solution 6 - AndroidShubham AgarwalView Answer on Stackoverflow
Solution 7 - AndroidgprathourView Answer on Stackoverflow
Solution 8 - AndroidRe'emView Answer on Stackoverflow
Solution 9 - AndroidKirill VashiloView Answer on Stackoverflow
Solution 10 - Androidjk7View Answer on Stackoverflow
Solution 11 - AndroidJustinView Answer on Stackoverflow
Solution 12 - AndroidVolodymyrView Answer on Stackoverflow
Solution 13 - AndroidSamuelView Answer on Stackoverflow
Solution 14 - AndroidAdityaView Answer on Stackoverflow
Solution 15 - AndroidAhsan Ullah RaselView Answer on Stackoverflow
Solution 16 - Androiduser3786340View Answer on Stackoverflow
Solution 17 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 18 - AndroidDarushView Answer on Stackoverflow
Solution 19 - AndroidAndrew.JView Answer on Stackoverflow
Solution 20 - Androidhas19View Answer on Stackoverflow
Solution 21 - AndroidMakaleleView Answer on Stackoverflow
Solution 22 - AndroidMark NashatView Answer on Stackoverflow
Solution 23 - AndroidFahmi EshaqView Answer on Stackoverflow
Solution 24 - AndroidRay LeeView Answer on Stackoverflow
Solution 25 - Androidlynn8570View Answer on Stackoverflow
Solution 26 - AndroidabbyView Answer on Stackoverflow
Solution 27 - AndroidRick PastoorView Answer on Stackoverflow