Android Html.fromHtml(String) doesn't work for <font color='#'>text</font>

Html.fromHtml("<font color='#145A14'>text</font>");

Instead of above please use following

Html.fromHtml("<![CDATA[<font color='#145A14'>text</font>]]>");

This worked for me and I am sure it will also work for you.

Let me know in case of any issue.


My answer involves guesswork about your code, but here goes:

When you use the font tag: DO NOT include an alpha channel so that your hex string looks like "#ff123456". If you use Integer.toHexString(), you will have an alpha channel in that result.

It worked when i used substring(2) on my hex string from rescource.

To sum up:

text.setText(Html.fromHtml("<font color='#123456'>text</font>"));

will work, but:

text.setText(Html.fromHtml("<font color='#ff123456'>text</font>"));

won't!


Make sure to turn off any modifiers like:

android:textAllCaps="true"

The fromHtml method is extremely limited in terms of the HTML tags that it supports, and font is not one of them. See http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html for an unofficial list. I did some research on this myself, and I found that fromHtml is based on an obscure and poorly documented rendering engine.