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