Can I underline text in an Android layout?


Question

How can I define underlined text in an Android layout xml file?

1
623
10/11/2018 4:16:40 AM

Accepted Answer

It can be achieved if you are using a string resource xml file, which supports HTML tags like <b></b>, <i></i> and <u></u>.

<resources>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>

If you want to underline something from code use:

TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
1063
10/25/2017 1:57:11 PM

You can try with

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon