3

I'm in the process of creating an app for android, and one of the things it does is take existing Html on a server and uses TextView to display it to the user. Most Html tags are fine (e.g. bold), but things like unordered lists (aka bullets) are not rendered properly when using Html.fromHtml(txt)

I noticed that a BulletSpan exists in the android docs. However, there is absolutely no explanation as to how to use it properly.

Will the BulletSpan indeed help me, and how can I go about using it?

Finally, if it won't help me, how can I go about changing all the lists to have asterisks in the front of them (like markdown does) in Java?

Please don't answer to use webview.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
yydl
  • 24,284
  • 16
  • 65
  • 104

2 Answers2

2

Looking at the source code for android.text.Html (available at here), it looks like the following tags are converted by fromHtml(String).

  • br, p, div, em, b, strong, cite, dfn, i, big, small, font, blockquote, tt, a, u, sup, sub, h1, h2, h3, h4, h5, h6, img

Some of these tags have additional attributes that are converted. For example, the following values for the font color attribute are converted.

  • aqua, black, blue, fuchsia, green, grey, lime, maroon, navy, olive, purple, red, silver, teal, white, yellow

Unfortunately, I've not found where this information is documented.

To transform additional tags, such as ul and li, you'll need to supply an appropriate implementation of Html.TagHandler, for which Google searching turns up examples.

This hopefully is enough information on how to handle the translation. Whether to use BulletSpan, I cannot offer any advice, as I don't know that I've ever seen one in action, and Google searching for android "BulletSpan example" turns up zero matches. Internally in the non-public class android.content.res.StringBlock, BulletSpan is used to represent a list item. So, its use looks promising.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
1

I've struggled with the limited control that Html.fromHtml() gives you and I finally gave up.

I was already using HtmlCleaner to clean up the Html before feeding it to Html.fromHtml() and I just implemented a direct conversion to a Spanned. This way I had full control over how elements were rendered, though I did copy a lot from the original Html class.

HtmlCleaner will parse Html to a tree of TagNode objects, and I implemented a CleanHtmlParser.fromTageNode() method. It's here:

https://github.com/NightWhistler/PageTurner/blob/master/src/net/nightwhistler/pageturner/html/CleanHtmlParser.java

BulletSpan gave me some mixed results because it renders the bullets on the left of the line and then indents the text instead of indenting both text and bullets. I just wrapped them in a margin and printed a unicode bullet character in front of the text.

NightWhistler
  • 326
  • 2
  • 7
  • Do you need to use HtmlCleaner on your HTML? Html.fromHtml() is using TagSoup to handle "dirty" HTML already, so it seems like your HTML will be processed twice. – Dandre Allison Jul 11 '12 at 23:24
  • By now it evolved into a seperate project, which is a drop-in replacement for Html.fromHtml(): http://github.com/NightWhistler/HtmlSpanner – NightWhistler Dec 04 '12 at 13:51
  • Your library looks sweet. I cannot wait to use it. – MinceMan Jun 04 '13 at 22:41