109

Html List tag not working in android TextView. This is my string content:

String str="A dressy take on classic gingham in a soft, textured weave of stripes that resembles twill.  Take a closer look at this one.<ul><li>Trim, tailored fit for a bespoke feel</li><li>Medium spread collar, one-button mitered barrel cuffs</li><li>Applied placket with genuine mother-of-pearl buttons</li><li>;Split back yoke, rear side pleats</li><li>Made in the U.S.A. of 100% imported cotton.</li></ul>";

I loaded it in a text view like this:

textview.setText(Html.fromHtml(str));

The output looks like a paragraph. What can I do? Is there any solution for it?

Edit:

webview.loadData(str,"text/html","utf-8");
peterh
  • 11,875
  • 18
  • 85
  • 108
Praveen
  • 90,477
  • 74
  • 177
  • 219

15 Answers15

165

As you can see in the Html class source code, Html.fromHtml(String) does not support all HTML tags. In this very case, <ul> and <li> are not supported.

From the source code I have built a list of allowed HTML tags:

  • br
  • p
  • div
  • em
  • b
  • strong
  • cite
  • dfn
  • i
  • big
  • small
  • font
  • blockquote
  • tt
  • monospace
  • a
  • u
  • sup
  • sub

So you better use WebView and its loadDataWithBaseURL method. Try something like this:

String str="<html><body>A dressy take on classic gingham in a soft, textured weave of stripes that resembles twill.  Take a closer look at this one.<ul><li>Trim, tailored fit for a bespoke feel</li><li>Medium spread collar, one-button mitered barrel cuffs</li><li>Applied placket with genuine mother-of-pearl buttons</li><li>;Split back yoke, rear side pleats</li><li>Made in the U.S.A. of 100% imported cotton.</li></ul></body></html>";
webView.loadDataWithBaseURL(null, str, "text/html", "utf-8", null);
Cristian
  • 198,401
  • 62
  • 356
  • 264
140

I was having the same problem, and what I did is overriding the default TagHandler. This one worked for me.

public class MyTagHandler implements TagHandler {

    boolean first = true;
    String parent = null;
    int index = 1;
    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        
        if (tag.equals("ul")) {
            parent = "ul";
        } else if (tag.equals("ol")) {
            parent = "ol";
        }

        if (tag.equals("li")) {
            if (parent.equals("ul")) {
                if (first) {
                    output.append("\n\t•");
                    first = false;
                } else {
                    first = true;
                }
            } else{
                if (first) {
                    output.append("\n\t"+index+". ");
                    first = false;
                    index++;
                } else {
                    first = true;
                }
            }   
        }
    }
}

and for displaying the text...

myTextView.setText(Html.fromHtml("<ul><li>I am an Android developer</li><li>Another Item</li></ul>", null, new MyTagHandler()));

[Edit]

Kuitsi has also posted an really good library that does the same, got it from this SO link.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Aman Gautam
  • 3,549
  • 2
  • 21
  • 25
  • We used this approach in the end. Any unsupported HTML tags, we code in text ourselves. For now its just ol and ul, but we added in stacks to handle nesting of lists and storing indexes when nesting ol. In addition you can use the opening boolean parameter in replacement of first. – JonWillis Jun 14 '12 at 11:43
  • 6
    @Aman Gautam very awesome thanks for this! Do you have any idea how to tab the text when it wraps more than 1 line? With this code after the 2nd line the text is aligned with the number rather than tabbed in to keep the number separate. I attempted a few things but I could not figure it out – RyanG Nov 21 '12 at 22:11
  • same thing here, linebreaks in a list causes trobule with this approach. – Andreas Rudolph Jan 14 '13 at 16:04
  • Instead of using the pasted bullet character, it may be better to use the unicode character: output.append("\n\t\u2022"); – Matt McMinn Mar 10 '13 at 17:14
  • Thank you for this nice code, but I can't use it until we'll find a solution how to fix the multiple line indentation – peter.bartos Jul 17 '13 at 08:26
  • This was the correct way to obtain the results I wanted. Thanks! – Machine Tribe Jan 18 '16 at 13:52
  • There's a crash you can get from this code snipped if the parent is null when you reach `if (parent.equals("ul"))`. This can occur when an `
  • ` doesn't have either a `
      ` or `
      ` parent which isn't that uncommon of a scenario
  • – alexgophermix Jan 31 '17 at 17:23
  • @MartinPfeffer The answer was written 5+ years back. If something has changed in that time, please update the answer. I don't work on Android anymore. – Aman Gautam Jul 30 '17 at 20:04
  • TagHandler is used for tags which are not supported by platform. Now
      ,
      ,
    1. are supported, so I recommend you to make custom tags inside your html string(file) for examplel: , , .
    – Ban Markovic Aug 08 '19 at 13:55