Skip to main content

Underline TextView Text - Android

If your app need an underline for the text in TextView

 Achieve it in simple way  - (By both xml and code part)



xml

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>

Code
Way - 1
TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Hello, Android !!!");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
Way - 2
textview.setPaintFlags(textview.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
Way - 3
textview.setText(Html.fromHtml("<u>Text to underline</u>"));





Comments