I have String with ul and li in it. And I am trying to show them in HTML formatting in textview. textView.setText(Html.fromHtml(myHtmlText));
But textview shows the plain text. How can I have the ul and li tags formatted in textview?
You can use Html.TagHandler.
In your case it will be like this:
public class UlTagHandler implements Html.TagHandler{ @Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { if(tag.equals("ul") && !opening) output.append("\n"); if(tag.equals("li") && opening) output.append("\n\t•"); } }
and
textView.setText(Html.fromHtml(myHtmlText, null, new UlTagHandler()));
Tags Supported in String Resources
Tags in static string resources are parsed by android.content.res.StringBlock, which is a hidden class. I've looked through the class and determined which tags are supported:
<a> (supports attributes "href") <annotation> <b> <big> <font> (supports attributes "height", "size", "fgcolor" and "bicolor", as integers) <i> <li> <marquee> <small> <strike> <sub> <sup> <tt> <u>
Tags Supported by Html.fromHtml()
For some reason, Html.fromHtml() handles a different set of of tags than static text supports. Here's a list of the tags (gleaned from Html.java's source code):
<a> (supports attribute "href") <b> <big> <blockquote> <br> <cite> <dfn> <div> <em> <font> (supports attributes "color" and "face") <i> <img> (supports attribute "src". Note: you have to include an ImageGetter to handle retrieving a Drawable for this tag) <p> <small> <strong> <sub> <sup> <tt> <u>
see this link for more details
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