I'd like to have Spannable that looks like error in IDEs - underline with another color.
I've tried to create ColorUnderlineSpan
class that extends android UnderlineSpan
, but it makes all the text another color (i need to add colored underline only):
/**
* Underline Span with color
*/
public class ColorUnderlineSpan extends android.text.style.UnderlineSpan {
private int underlineColor;
public ColorUnderlineSpan(int underlineColor) {
super();
this.underlineColor = underlineColor;
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(underlineColor);
}
}
I've also found DynamicDrawableSpan
class but i can't see canvas bounds to draw to.
It would be great to get any Spannable impl with abstract draw method with bounds argument.
This isn't the prettiest solution, but it ended up working for me:
It's weird because you have to specify the character index to start and end your underlining with, but it worked for me.
The answer @korbonix posted works fine. I've made some improvements in Kotlin and supporting multiline TextViews:
And here is a sample usage. textText is the TextView. The text is 127 characters long and it underlines from the position 112 to the 127.
Important: For reasons I don't fully understand the span length should be set to the full length of the text. Otherwise the component doesn't even get called. Feel free to educate me on why's that.