50

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.

3
  • 3
    the problem was when i did textView.append("\n"+Html.formHtml("<font color='#145A14'>text</font>"); the \n character created the new line, but the formed html with the color was not received by the textview -- it instead used its default color. I had to use the html linebreak for new lines: Html.fromHtml("<font color='#145A14'>text</font><br>"); Commented Jun 20, 2011 at 17:50
  • 2
    I suggest you write your comment as an answer and accept your own answer. It will make it easier for others to see the solution. Commented Nov 21, 2012 at 14:13
  • Check out for working example javatechig.com/2013/04/07/how-to-display-html-in-android-view Commented Apr 7, 2013 at 20:46

12 Answers 12

61
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.

Sign up to request clarification or add additional context in comments.

3 Comments

@PratikButani You can get the string in separate string and finally can set from html. ex> strtoformat = "<font color='#145A14'>variablename</font>" and then can load as Html.fromHtml(strtoformat); simple :-)
It working android os 4.3 but It's not working android os 5 and above.
@user533787 use HtmlCompat and it will be backward compatible
26

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!

1 Comment

this good answer, however note that it would not work also for android:textAllCaps="true". This is probably some kind of bug in Android.
24

Make sure to turn off any modifiers like:

android:textAllCaps="true"

2 Comments

The default is true with the latest appCompact, so you need to manually set it to false.
what is the reason behind this?
5

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.

3 Comments

That list seems to include both the font tag and the color attribute as ones supported by the Html.fromHtml() method?
+1 for providing the link with the supported tags and their attributes.
that list doesn't cover <span> and <font>, which some tablets support in practice...
4

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();
}

Comments

2

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>"));

Comments

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

Comments

2
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.

Comments

1

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

Comments

1

try this and it should works

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

1 Comment

First thing : i think that my answer is the same as others, so you have voted down for my answer .Second thing : notice that i've used \"#145A14\" and NOT '#145A14' .
0

Yeah I agree, it doesn't work sometimes.

As an alternative, I use in xml for Textview:

android:textColorLink="yourColor"

works like a charm ;)

1 Comment

it will not work if the spanable got converted to string somehow took and hour for me to figure it out. use textView.setText(Html.fromHtml("<font color=\"#145A14\">text</font>")); and then append and prepend any other strings inside the fromHTML brakets.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.