Android: Statically underline dynamic text in TextView

The easiest solution is probably to create a custom UnderLineTextView component deriving from TextView, override setText() and set the entire text as underlined, something like this (underline code from the link you referred to above):

@Override
public void setText(CharSequence text, BufferType type) {
    // code to check text for null omitted
    SpannableString content = new SpannableString(text);
    content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
    super.setText(content, BufferType.SPANNABLE);

}

It’s then just a matter of using your new component in the layout and setting the text as usual. The rest is handled automatically.

More info on custom components:
http://developer.android.com/guide/topics/ui/custom-components.html

You can underline text over Html.fromHtml(String source)

Example:

textView.setText(Html.fromHtml("this is <u>underlined</u> text"));

Leave a Comment