Option 1 is probably most straightforward and most of the hard work for it has already been done before. You've got the general idea correct: after the HTML has been processed, you can request all the generated URLSpan
instances and loop through them. You can then replace it with a customized clickable span to get full controls over any of the span clicks.
In the example below, I'm just replacing every URLSpan
with a simple extension of that class that takes the original url (actually, I should probably say 'uri') and replace its scheme part. I've left the actual onClick()
logic unimplemented, but I'll leave that up to your imagination.
SpannableStringBuilder builder = ...
URLSpan[] spans = builder .getSpans(0, builder .length(), URLSpan.class);
for (URLSpan span : spans) {
int start = builder .getSpanStart(span);
int end = builder .getSpanEnd(span);
s.removeSpan(span);
span = new CustomURLSpan(span.getURL().replace("http://", "scheme://"));
s.setSpan(span, start, end, 0);
}
textView.setText(builder);
As mentioned earlier, here the CustomURLSpan
class is a simple extension of URLSpan
that takes a url and overrides the onClick()
method so our own logic can be executed there.
public class CustomURLSpan extends URLSpan {
public CustomURLSpan(String url) {
super(url);
}
@Override public void onClick(View widget) {
// custom on click behaviour here
}
}
Some related Q&A's that basically do a similar thing (might be helpful for some more inspiration):