Reputation: 15057
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?
Upvotes: 4
Views: 2864
Reputation: 949
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");
Upvotes: 0
Reputation: 387
String Text = "<u><font color='blue'>Underline Text</font></u>.";
textView.setText(Html.fromHtml(Text), TextView.BufferType.SPANNABLE);
Upvotes: -1
Reputation: 15057
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);
}
}
}
Upvotes: 8
Reputation: 1367
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.");
Upvotes: 0