Chris
Chris

Reputation: 511

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.

Upvotes: 50

Views: 66970

Answers (12)

Garg
Garg

Reputation: 2731

For color text with hyperlink URL:

textView.setText(Html.fromHtml("You agree to our <font color='#F20000'><a href='https://www.yoururl.com'> Terms of Service</a></font> and <font color='#F20000'><a href='https://www.yoururl.com'>Privacy Policy</a></font>", Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);

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

Upvotes: 0

Naina
Naina

Reputation: 59

txt_description1.setText(Html.fromHtml("<font color='rgb'>"+str_description1+"</font>"));

If you do not want a single static color and want to directly reflect from editor you can use "rgb". It will reflect the exact color what you have set in editor, just set in textview and concat it with textview value. And you are all set to go.

Upvotes: 2

Bipin Vayalu
Bipin Vayalu

Reputation: 3145

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.

Upvotes: 60

Cabezas
Cabezas

Reputation: 10727

I use this code

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

public String convertToHtml(String htmlString) {

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("<![CDATA[");
    stringBuilder.append(htmlString);
    stringBuilder.append("]]>");
    return stringBuilder.toString();
}

Upvotes: 4

xstmpx
xstmpx

Reputation: 646

Make sure to turn off any modifiers like:

android:textAllCaps="true"

Upvotes: 24

abhishekm473
abhishekm473

Reputation: 109

Yeah I agree, it doesn't work sometimes.

As an alternative, I use in xml for Textview:

android:textColorLink="yourColor"

works like a charm ;)

Upvotes: 0

Houcine
Houcine

Reputation: 24181

try this and it should works

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

Upvotes: 1

Lenny T
Lenny T

Reputation: 409

Make sure your RGB value is CAPITALIZED. Android can understand #00FF00 but not #00ff00.

Upvotes: 1

Andreas Rudolph
Andreas Rudolph

Reputation: 1246

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!

Upvotes: 26

Devendra Anurag
Devendra Anurag

Reputation: 21

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

Upvotes: 2

Konstantin Burov
Konstantin Burov

Reputation: 69238

That looks like a very dark color, are you sure that your screen is capable to display such colors, so you can distinguish them from black? The code snippet looks good, I've tried similar code many times and it worked like a charm. Try it with somewhat brighter, i.e. #ff0000 (red), to verify that it works:

TextView text = ... // find or instantinate your text view.
text.setText(Html.fromHtml("<font color='#ff0000'>text</font>"));

Upvotes: 2

Philip Sheard
Philip Sheard

Reputation: 5825

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.

Upvotes: 5

Related Questions