4

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,997
  • 13
  • 42
  • 62

4 Answers4

8

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,805
  • 19
  • 81
  • 156
Rakesh
  • 14,997
  • 13
  • 42
  • 62
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,339
  • 12
  • 22
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
  • 941
  • 6
  • 26
-1
String Text = "<u><font color='blue'>Underline Text</font></u>.";
textView.setText(Html.fromHtml(Text), TextView.BufferType.SPANNABLE);
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Muhammad
  • 339
  • 3
  • 22