41

I have a button which is transparent and has an icon and text.

I want to underline the text of the button but i have not been able to do this.

Below is my xml code:

<Button
     android:id="@+id/parkButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:drawableStart="@drawable/park"
     android:text="@string/button_name"
     android:background="@android:color/transparent"
     android:textColor="@android:color/black"/>

And the String file has:

<resources>
    <string name="button_name"><u>Parking Areas</u></string>
</resources>

This approach works in TextView, but not in Button. Any suggestion?

Morgan Koh
  • 2,297
  • 24
  • 24
user3303274
  • 729
  • 2
  • 8
  • 21
  • 1
    Please have a look http://stackoverflow.com/questions/8033316/to-draw-an-underline-below-the-textview-in-android/8033336#8033336 – Nivedh Jul 30 '15 at 08:32
  • Yes, i had already gone through this article. This is for textview in which case this works perfectly. But same method is not working for button text – user3303274 Jul 30 '15 at 08:34

6 Answers6

86

Code only

Java:

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

Kotlin:

val button = findViewById<Button>(R.id.park);
button.paintFlags = button.paintFlags or Paint.UNDERLINE_TEXT_FLAG

Resource string with static text (xml only)

If you have a static text in your resources you could also use the following approach in your strings.xml:

<string name="underlined_text"><u>I\'m underlined</u></string>

Resource string with dynamic text (xml + code)

If you're using dynamic text but don't like the first approach (which isn't the best imho either), you could also use following:

strings.xml

<string name="underlined_dynamic_text"><u>%s</u></string>

Java:

button.setText(getString(R.string.underlined_dynamic_text, "I'm underlined");

Kotlin:

button.text = getString(R.string.underlined_dynamic_text, "I'm underlined")
finki
  • 2,063
  • 1
  • 20
  • 23
  • 2
    Took me a long time to come back ! But gaining more experience on Android by now, I realized that this solution would have worked perfectly fine. Thanks for the help. – user3303274 Apr 29 '16 at 10:00
16

This should make your ButtonText bold, underlined and italic at the same time.

strings.xml

<resources>
    <string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>

To set this String to your TextView, do this in your main.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/register" />
Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
6

You can't set underline from xml file. To set underline using code you need to set the underline flag on button.

Button button = (Button) findViewById(R.id.park);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Amit Padekar
  • 259
  • 1
  • 6
3

Use this:

TextView txt=(TextView)findViewById(R.id.txt);
        String styledText = "<u>parking areas</u>";
        txt.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
Ketan
  • 423
  • 3
  • 12
3
Button button= (Button) findViewById(R.id.park);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
button.setText(content);
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

With Compose you can use the textDecoration attribute in the Text:

Button(
    onClick = {}
) {
    Icon(
        Icons.Filled.Add,
        contentDescription = "Add",
    )
    Text(
        text = "Button",
        textDecoration = TextDecoration.Underline
    )
}

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841