Kushal Shah
Kushal Shah

Reputation: 1333

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.

Upvotes: 10

Views: 6836

Answers (4)

Abir Hasan Shawon
Abir Hasan Shawon

Reputation: 6119

Really late to encounter this scenrio. Here's another way, It would be to set multiple spans to the same spannable content:

SpannableString content = new SpannableString("Ack:");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
content.setSpan(
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)),
        0,
        content.length(),
        0
);
tvAck.setText(content, TextView.BufferType.SPANNABLE);

Upvotes: 0

Gnod
Gnod

Reputation: 155

In TextPaint, there has a field 'underlineColor' and method 'setUnderlineText', indicated and can use to changed the underline color. But, they are '@hide' field and method, to use them, you must using reflecting, like this:

Field field = TextPaint.class.getDeclaredField("underlineColor");
field.setAccessible(true);
field.set(ds, mUnderlineColor);

ds is your TextPaint Object.

Upvotes: 1

kennytm
kennytm

Reputation: 523264

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);
        }
    }
}

Upvotes: 15

Aleks G
Aleks G

Reputation: 57316

I haven't tried this myself, so this is more an idea than a solution, but probably worth trying. Class UnderlineSpan has method updateDrawState, which takes TextPaint as a parameter. In turn, TextPain can has field public int linkColor.

So for you it would be something like

TextPaint tp = new TextPaint();
tp.linkColor = [your color];           //not quite sure what the format should be
UnderlineSpan us = new UnderlineSpan();
us.updateDrawState(tp);
SpannableString content = new SpannableString("Ack:");
content.setSpan(us, 0, content.length(), 0); tvAck.setText(content);

Reference for both TextPaint and UnderlineSpan are very poor, with majority of javadoc missing altogether (judge for yourself: http://developer.android.com/reference/android/text/TextPaint.html), so I'm not certain how to use these though.

Upvotes: 3

Related Questions