Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Underline Text of Button in Android?

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?

like image 679
user3303274 Avatar asked Jul 30 '15 08:07

user3303274


People also ask

How do you underline text buttons?

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.

How do you underline text on Android?

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

How do you underline text on phone?

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.

How do you underline text in XML?

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.


1 Answers

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") 
like image 65
finki Avatar answered Sep 19 '22 17:09

finki