Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting HTML Bullets to PlainText

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.

like image 764
yydl Avatar asked Jan 28 '26 22:01

yydl


2 Answers

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.

like image 65
Programmer Bruce Avatar answered Jan 30 '26 13:01

Programmer Bruce


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.

like image 36
NightWhistler Avatar answered Jan 30 '26 13:01

NightWhistler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!