10

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.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Kushal Shah
  • 1,303
  • 2
  • 17
  • 33

4 Answers4

14

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);
        }
    }
}
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
3

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.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • I can't think of anything else. Why won't it work? Did you try it? – Aleks G Oct 07 '11 at 08:34
  • The reason that this does not work is that `updateDrawState` only tells the `TextPaint` that it should be underlined. It does not affect the underline color. An `UnderlineSpan` just uses the text color for the underline color. The `linkColor` would change the color of the underline (and text) only for a `ClickableSpan` (or `URLSpan`). – Suragch Oct 06 '17 at 06:49
1

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.

Gnod
  • 147
  • 4
0

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);
Abir Hasan Shawon
  • 6,069
  • 5
  • 17
  • 28