Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show <ul> <li> in android textview

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?

like image 260
aman.nepid Avatar asked Jun 26 '12 18:06

aman.nepid


2 Answers

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())); 
like image 110
Pavel Avatar answered Sep 20 '22 23:09

Pavel


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

like image 35
K_Anas Avatar answered Sep 22 '22 23:09

K_Anas