I am using an TextView to display an HTML string such as:
"Test HTML < a href=\"www.type1.com\">link1< /a> < a href=\"www.type2.com\">link2< /a>"
As you see, there are two different kinds of tags that I need to handle, so I need to be able to handle the two different kinds of tags and read the href attribute.
I tried using Html.TagHandler:
private class MyTagHandler implements Html.TagHandler {
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
Toast.makeText(getContext(), tag, Toast.LENGTH_LONG).show();
}
}
However the handleTag is not called on the < a > tag. I did tests and figure out it only has effect on the customized tags. Is it possible to also handle the stardard tags?
The goal of a Html.TagHandler custom implementation is to provide handling of tags that are not handled by the android framework. So in order to do what you want, one workaround is to replace all the tags that you want to handle with another tag that you know the framework won't handle, so it will enter into your implementation. For example, you could do a method like this one to prepare your html:
public string prepareHTMLForTagHandling(string htmlSource)
{
if (htmlSource == null || htmlSource == "")
return null;
return htmlSource.replace("<a", "<acustomlink")
.replace("</a>", "<acustomlink>");
}
And then use it like:
Html.fromHtml(prepareHTMLForTagHandling(myHtml), null, myHtmlCustomTagHandler);
Finally, in your custom tag handler implementation you handle "acustomlink" as a tag instead of "a".
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With