Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to add lines with different colors to my TextView using html tags.

For whatever reason,

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

won't show up colored in the TextView.

like image 855
Chris Avatar asked Jun 19 '11 04:06

Chris


2 Answers

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.

like image 83
Bipin Vayalu Avatar answered Sep 27 '22 19:09

Bipin Vayalu


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!

like image 44
Andreas Rudolph Avatar answered Sep 27 '22 20:09

Andreas Rudolph