I am developing the android application. I need to underline some of the Textview.
SpannableString content = new SpannableString("Ack:");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tvAck.setText(content);`
I have used the above code for that. But now i want to change the color of the underline. Can any one tell me how to do so. Any help or suggestion is accepted.
There is no documented method to set the underline color. However, there is an undocumented TextPaint.setUnderline(int, float)
method which allows you do provide the underline color and thickness:
final class ColoredUnderlineSpan extends CharacterStyle
implements UpdateAppearance {
private final int mColor;
public ColoredUnderlineSpan(final int color) {
mColor = color;
}
@Override
public void updateDrawState(final TextPaint tp) {
try {
final Method method = TextPaint.class.getMethod("setUnderlineText",
Integer.TYPE,
Float.TYPE);
method.invoke(tp, mColor, 1.0f);
} catch (final Exception e) {
tp.setUnderlineText(true);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With