29

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?

aman.nepid
  • 2,864
  • 8
  • 40
  • 48
  • KIndly check this post: [Stack Overflow Post][1] [1]: http://stackoverflow.com/questions/2116162/how-to-display-html-in-textview – Aqif Hamid Jun 26 '12 at 18:47
  • 2
    possible duplicate of [Html List tag not working in android textview. what can i do?](http://stackoverflow.com/questions/3150400/html-list-tag-not-working-in-android-textview-what-can-i-do) – kabuko Jun 26 '12 at 18:55

7 Answers7

52

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()));
Pavel
  • 642
  • 7
  • 13
13

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

K_Anas
  • 31,226
  • 9
  • 68
  • 81
6

see HTML Tags Supported By TextView both ul and li tags are not Supported by android.text.Html class.

SOLUTION : you can display these tags using WebView or by TagHandler

for more help see this post:

Html List tag not working in android textview. what can i do?

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
4

According to the answer below, you can not use every HTML tag here;

https://stackoverflow.com/a/3150456/2275962

But, sure, there ise a way of showing bullets in Android TextView. You can replace <li> tags with &#149; (which is HTML code for bullet).

If you want to try other list icons, use the preferred one from the table is this link;

http://www.ascii-code.com/

Community
  • 1
  • 1
Taner
  • 4,511
  • 3
  • 18
  • 14
3

A really easy way to make things work for API level M (23) and lower From your HTML string you can use the following:

private fun String.formattedHtml(): String {
    // Work around for API Versions lower than N, Html.fromHtml does not render list items correctly
    return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        this.replace("<ul>", "")
            .replace("</ul>", "")
            .replace("<li>", "<p>• ")
            .replace("</li>", "</p>")
    } else {
        this
    }
}

Then, you can use:

Html.fromHtml("<ul><li> test </li></ul>".formattedHtml())

No need for TagHandler for API N and up, as mentioned in https://stackoverflow.com/a/40524835, android.text.Html supports li and ul tags.

jameswoo
  • 386
  • 3
  • 12
1

Im my case my data is coming down from an API call. I still needed the output to match the format of my app so I wrap the HTML string in formatting tags to display in my WebView:

public static String formatHtmlString(String stringIn) {

    String format = "<html>"
            + "  <head>"
            + "    <style type='text/css'>"
            + "      body { "
            + "           font-family: \"HelveticaNeue-Light\", \"Helvetica Neue Light\"; "
            + "           font-size:14px;" + "           color:#000;"
            + "      }" + "      b { " + "           font-size:14px;"
            + "       }" + "      i { " + "           font-size:10px;"
            + "           color:#333333;" + "           opacity:0.75;"
            + "       }" + "    </style>" + "  </head>" + "  <body>"
            + stringIn + "</body>" + "</html>";

    return format;
}
Shane
  • 46
  • 3
0

In my case, I'm getting a list of String (from backend as a JSON Response), then render the HTML manually using this approach for TextView only:

In Kotlin

// ViewUtils.kt
@file:JvmName("ViewUtils")

package com.company

private fun List<String>.joinToBulletList(): String =
        joinToString(prefix = "<ul>", postfix = "</ul>", separator = "\n") {
            "<li> $it</li>"
        }

fun TextView.renderToHtmlWithBulletList(contents: List<String>) {
    val content = contents.joinToBulletList()

    val html: Spannable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT, null, null) as Spannable
    } else {
        Html.fromHtml(content, null, null) as Spannable
    }

    text = html
}

// AnyActivity.kt
val contents = listOf("desc 1", "desc 2", "desc 3")
textViewFromXml.renderToHtmlWithBulletList(contents)

In Java

// AnyActivity.java
List<String> contents = new String["desc 1", "desc 2", "desc 3"];
ViewUtils.renderToHtmlWithBulletList(textViewFromXml, contents)

Notice the @file:JvmName("ViewUtils") it is useful to change the file name so Java code can call it ViewUtils.java instead of ViewUtilsKt.java. See here for more detail

P.S: Please feel free to edit this

mochadwi
  • 1,190
  • 9
  • 32
  • 87