I have a TextView that displays some HTML code (images included, ImageGetter).
The html is not mine, but I can ask them to include custom scheme links, maybe even tags.
Purpose: to display some dynamically generated content without the need to play with nested Android layouts.
Problem: some links must be handled in the application (new Fragment loaded).
Can't use a receiver for action.VIEW, since it's an Activity Intent, not a broadcast, and its use will be very contextual, so only a programmatically registered receiver would do.
I'm usingtextView.setText(Html.fromHtml(content.content, imageGetter, null)
. need everything to remain the same, except some spans should have my own onClick
on it. I'm not very familiar with Spanned, so I see these options:
- Edit the SpannableStringBuilder returned from
Html.fromHtml
, and replace the URLSpans I want with a custom ClickableSpan (how?) - As above, but copy everything to a new builder, exchanging the URLSpans for my own (how?
append
takes a CharSequence only, and I get RelativeSizeSpan, StyleSpan, ImageSpan, URLSpan...) - Create a Spanned manually. I can do it for the custom scheme, but how to duplicate the effect of
Html.fromHtml
(or close enough) for all else?
[edit]
Thanks to MH. for the info. I had tried that before, but failed. Now that i returned to it, I found i had made an error, passing the wrong item to the 1st argument of setSpan.
If anyone's interested, i now use this:
public static interface OnSpanClickListener {
void onClick(String url);
}
public static SpannableStringBuilder getSpannable(String source, ImageGetter imageGetter, String scheme, final OnSpanClickListener clickHandler){
SpannableStringBuilder b = (SpannableStringBuilder) Html.fromHtml(source, imageGetter, null);
for(URLSpan s : b.getSpans(0, b.length(), URLSpan.class)){
String s_url = s.getURL();
if(s_url.startsWith(scheme+"://")){
URLSpan newSpan = new URLSpan(s_url.substring(scheme.length()+3)){
public void onClick(View view) {
clickHandler.onClick(getURL());
}
};
b.setSpan(newSpan, b.getSpanStart(s), b.getSpanEnd(s), b.getSpanFlags(s));
b.removeSpan(s);
}
}
return b;
}
(...)
body.setText(getSpannable(content.content, imageGetter, getResources().getString(R.string.poster_scheme), new OnSpanClickListener(){
public void onClick(String url) {
// do whatever
}
}));