Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of underline in android

I am trying to change the color of underline in textView,I came across the link

How to get UnderlineSpan with another color in Android?

But if i tried to implement that,I am not getting color

This is my code

String middleStringText = MyTextView.getText().toString();
Spannable spannable1 = new SpannableString(middleStringText);
CustomUnderLineSpan underLineSpan = new CustomUnderLineSpan(Color.YELLOW,2, 5);
spannable1.setSpan(underLineSpan, 0, 10, spannable1.SPAN_EXCLUSIVE_EXCLUSIVE);
MyTextView.setText(spannable1, TextView.BufferType.SPANNABLE);

Have anyone tried similar sort of implementation?

like image 574
Rakesh Avatar asked Oct 28 '25 14:10

Rakesh


1 Answers

It is not correct Solution,But it is useful for time being purpose,which i got from some link in stack over flow.

spannable1.setSpan(new ColoredUnderlineSpan(Color.YELLOW), middleStringText.indexOf(startText), middleStringText.indexOf(EndText) + value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


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, 8.0f);
        } catch (final Exception e) {
            tp.setUnderlineText(true);
        }
    }
}
like image 123
Rakesh Avatar answered Oct 30 '25 04:10

Rakesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!