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 tex
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
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?
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.
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;
}
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
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()));