Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of the underline in android

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.

like image 402
Kushal Shah Avatar asked Oct 07 '11 08:10

Kushal Shah


1 Answers

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);
        }
    }
}
like image 65
kennytm Avatar answered Oct 25 '22 17:10

kennytm