Having the textView with autoLinkMask
set to Linkify.ALL
, i\'m able to open the links and the browser shows the web-page.
I need to call a
Step #1: Create your own subclass of ClickableSpan
that does what you want in its onClick()
method (e.g., called YourCustomClickableSpan
)
Step #2: Run a bulk conversion of all of the URLSpan
objects to be YourCustomClickableSpan
objects. I have a utility class for this:
public class RichTextUtils {
public static Spannable replaceAll(Spanned original,
Class sourceType,
SpanConverter converter) {
SpannableString result=new SpannableString(original);
A[] spans=result.getSpans(0, result.length(), sourceType);
for (A span : spans) {
int start=result.getSpanStart(span);
int end=result.getSpanEnd(span);
int flags=result.getSpanFlags(span);
result.removeSpan(span);
result.setSpan(converter.convert(span), start, end, flags);
}
return(result);
}
public interface SpanConverter {
B convert(A span);
}
}
You would use it like this:
yourTextView.setText(RichTextUtils.replaceAll((Spanned)yourTextView.getText(),
URLSpan.class,
new URLSpanConverter()));
with a custom URLSpanConverter
like this:
class URLSpanConverter
implements
RichTextUtils.SpanConverter {
@Override
public URLSpan convert(URLSpan span) {
return(new YourCustomClickableSpan(span.getURL()));
}
}
to convert all URLSpan
objects to YourCustomClickableSpan
objects.