3

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?

Community
  • 1
  • 1
Rakesh
  • 14,607
  • 13
  • 37
  • 60

4 Answers4

7

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);
        }
    }
}
Leos Literak
  • 8,186
  • 16
  • 70
  • 132
Rakesh
  • 14,607
  • 13
  • 37
  • 60
0

What you can do is to create a new paint color and then assign that color to the textview.

Paint p = new Paint();
p.setColor(Color.RED);

TextView myTextView = (TextView) findViewById(R.id.textview);
myTextView .setPaintFlags(p.getColor());
myTextView .setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
myTextView .setText("Underline Text with red color.");  
Kristo
  • 1,317
  • 12
  • 20
0

Check This

        Paint p = new Paint();
        p.setColor(Color.RED);

        TextView t = (TextView) findViewById(R.id.textview);
        t.setPaintFlags(p.getColor());
        t.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
        t.setText("Hello World");  
Chandan kushwaha
  • 905
  • 6
  • 24
-1
String Text = "<u><font color='blue'>Underline Text</font></u>.";
textView.setText(Html.fromHtml(Text), TextView.BufferType.SPANNABLE);
Fruchtzwerg
  • 9,895
  • 12
  • 39
  • 45
Muhammad
  • 250
  • 3
  • 16