android: how to add Underline in the String

16,224

Solution 1

use SpannableString by which you can do Underline, Strike, Highlight, Bold, Italic etc to the text

for Underline you have to use UnderlineSpan http://developer.android.com/reference/android/text/style/UnderlineSpan.html

 TextView textview = (TextView)findViewById(R.id.textview);
 SpannableString content = new SpannableString("Apple Banana Grape Melon");
 content.setSpan(new UnderlineSpan(), 6, 11, 0);
 textview.setText(content);

or simply by adding HTML tag to the resource string <string name="sample"> <u>your data</u></string>

Solution 2

<resource>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
Share:
16,224
adig
Author by

adig

Updated on June 14, 2022

Comments

  • adig
    adig almost 2 years
    <string-array name="myarray">    
    <item>Apple</item> 
    <item>Banana</item> 
    <item>Grape</item>
    <item>Melon</item>
    

    if i want to add Underline the "Banana" what should I add?

  • adig
    adig about 12 years
    This string will I show the button text, so if the button is clicked it will display the button text "Apple", "Banana", "Grape" and appeared Underline the words "Banana"
  • Mohammed Azharuddin Shaikh
    Mohammed Azharuddin Shaikh about 12 years
    see updated answer, you have mold code according to requirement. I just shown you different way.