I am trying to add lines with different colors to my TextView using html tags.
For whatever reason,
Html.fromHtml(\"
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.
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();
}
textView.setText(Html.fromHtml("<font color='blue'>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.
Make sure your RGB value is CAPITALIZED. Android can understand #00FF00 but not #00ff00.
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.