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?
Style attributes specify the style that is expressed in an element for a straight line. You can employ this attribute with the HTML/CSS text-decoration attribute and use its attribute set. What Is The Button To Underline Text? If you wish to underline text, go to Home > Underline or press Ctrl+U to do so.
You can define the underlined text in an Android layout XML using a String Resouce XML file. In a string res file you have to use an HTML underline tag <u> </u> .
Once the feature is enabled you long hold over the text you want to underline. Then tap the button showing a “U” which stands for underline. This will underline the highlighted portion of your text message.
Just use <u> and <\u> in XML, and it's enough. 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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With