How to get rid of the underline in a Spannable String with a Clickable Object?
This works for me. No need to create custom ClickableSpan
class. Just override updateDrawState(TextPaint ds)
.
SpannableString span = new SpannableString("Some text");
ClickableSpan clickSpan = new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ds.linkColor); // you can use custom color
ds.setUnderlineText(false); // this remove the underline
}
@Override
public void onClick(View textView) {
// handle click event
}
};
span.setSpan(clickSpan, 5, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(span);
Use the below code and try
String mystring =" Hello";
SpannableString ss= new SpannableString(mystring);
ss.setSpan(new MyClickableSpan(mystring), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
class MyClickableSpan extends ClickableSpan{// extend ClickableSpan
String clicked;
public MyClickableSpan(String string) {
super();
clicked = string;
}
@Override
public void onClick(View tv) {
Toast.makeText(MainActivity.this,clicked , Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
}
}
Raghunandan's answer works perfectly for me. Here is a pared-down version of it:
public abstract class NoUnderlineClickableSpan extends ClickableSpan {
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}