Background
I wanted to use some URLSpan inside a textView, so I've used something like:
final Spanned spanned=Html.fromHtml(getString(R.string.test));
msgTextView.setText(spanned);
The problem
It works, but when clicking an item, it stays clicked. it's as if I still touch the link...
What I've tried
I've tried to clear the focus from the textView and to set the focus on another view, but none of those helped.
I've also tried to remove the UrlSpan and add a new one instead, each time it gets clicked, but it didn't work. Here's the code:
public static Spannable setOnLinkClickedListener(final Spanned original,final IOnLinkClickListener listener)
{
final SpannableString result=new SpannableString(original);
final URLSpan[] spans=result.getSpans(0,result.length(),URLSpan.class);
for(final URLSpan span : spans)
{
final int start=result.getSpanStart(span);
final int end=result.getSpanEnd(span);
final int flags=result.getSpanFlags(span);
result.removeSpan(span);
final String url=span.getURL();
result.setSpan(new CustomURLSpan(url,start,result,end,listener,flags),start,end,flags);
}
return result;
}
private static final class CustomURLSpan extends URLSpan
{
private final int _start;
private final SpannableString _result;
private final String _url;
private final int _end;
private final IOnLinkClickListener _listener;
private final int _flags;
private CustomURLSpan(final String url,final int start,final SpannableString result,final int end,final IOnLinkClickListener listener,final int flags)
{
super(url);
_start=start;
_result=result;
_url=url;
_end=end;
_listener=listener;
_flags=flags;
}
@Override
public void onClick(final View widget)
{
if(_listener==null||!_listener.onClick(widget,_url))
super.onClick(widget);
_result.removeSpan(this);
_result.setSpan(new CustomURLSpan(_url,_start,_result,_end,_listener,_flags),_start,_end,_flags);
}
}
The question
How do I clear the selection/clicking effect that stays on URLSpan each time it gets clicked?
this answer is work for me. I test in API19 API26 with
Checkbox
,CheckedTexView
TextView
all clarify