I am trying to change the color of the underline indicator in a TextView
.
I know I can underline text like this
Spanned htmlString = Html.fromHtml("<u> <font color=\"#FF0000\"> some text </font> </u>")
someTextView.setText(htmlString);
And like this
SpannableString content = new SpannableString(test);
content.setSpan(new UnderlineSpan(), 0, test.length(), 0);
tvTest.setText(content);
But both solutions use the same color for the text and the underline indicator. I want to know if I can change the color of the underline indicator in a sane way without any background XML and reflection hacks as seen here
I want to underline only the text, not the paddings, margins. My text can span over multiple lines too, I want every line to be underlined. (This is where the XML solution fails).
Result of my code
It feels weird answering my own question, but for the sake of anyone having the same problem, I will. I have stumbled upon Layout class when reading some other posts for doing this on
EditText
. It provides everything you need to make this happen by manually drawing underline with canvas.First I defined custom attributes for an easy customization in XML layout files
And a custom
TextView
classThen it's usage is simple
Final result