Display HTML format
First we will present below what are the supported tags for a textView and what are not supported. You can skip this section if you are already familiar with these or you want to jump directly into action :)
Supported tags |
Unsupported tags |
|
|
|
|
1. String in Java code
Examples that don’t work
- Using alpha channel
String str = "<a>This is a <font color='#800000FF'> blue text</font> and this is a <font color='red'> red text</font> </a>"; TextView textView = (TextView)findViewById(R.id.text); textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));
Note: 80 from #800000FF should have added a blue color with an alpha of 50% to the “blue” text
Examples that work
- Using rgb
String str = "<a>This is a <font color='blue'> blue text</font> and this is a <font color='red'> red text</font> </a>"; TextView textView = (TextView)findViewById(R.id.text); textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));
- Using hexa (it works with lower case letters also)
String str = "<a>This is a <font color='#0000FF'> blue text</font> and this is a <font color='red'> red text</font> </a>"; TextView textView = (TextView)findViewById(R.id.text); textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));
2. String in strings.xml
When the text that needs to be formatted is located in strings.xml, there are a few ways of getting the text. Unfortunately, not all of them will keep the style.
strings.xml
<resources> <string name="formatted_text">This is normal text. <font color='red'>This is a red text</font></string> </resources>
Examples that DON’T work
- using
getString()
TextView textView = (TextView)findViewById(R.id.text); textView.setText(getString(R.string.formatted_text));
- using
getString()
+Html.fromHtml()
TextView textView = (TextView)findViewById(R.id.text); textView.setText(Html.fromHtml(getString(R.string.formatted_text), Html.FROM_HTML_MODE_COMPACT));
Examples that work
- using
getText()
TextView textView = (TextView)findViewById(R.id.text); textView.setText(getText(R.string.formatted_text));
- using the resource id directly
TextView textView = (TextView)findViewById(R.id.text); textView.setText(R.string.formatted_text);
- Using
getString()
+Html.fromHtml()
+<![CDATA[html source code]]>
<![CDATA[html source code]]>
is used in strings.xml so that you can use the actual HTML you include without needing to translate all the special characters like <, >, etc.
<resources> <string name="formatted_text"><![CDATA[ This is normal text. <font color=red>This is a red text</font>]]></string> </resources>
TextView textView = (TextView)findViewById(R.id.text); textView.setText(Html.fromHtml(getString(R.string.formatted_text), Html.FROM_HTML_MODE_COMPACT));
Note: CDATA is working only if the string is retrieved using Html.fromHtml()
.
Note 2: Also notice that when we used CDATA color=red is written without ”. If you keep ” they need to be escaped.
What is the difference between getText() and getString()?
getText()
returns CharSequencegetString()
returns String
Below is a table in which are presented CharSequence interface and some of the classes that implement this interface.
As you could see from the above table, String objects do not keep style formatting, so this is why calling getString()
won’t work. On the other hand, getText()
internally uses Html.fromHtml()
to parse HTML tags, which returns a Spanned object, which keeps the style formatting of a text.
- alpha channel
<resources> <string name="formatted_text">This is normal text. <font color='#800000FF'>This is a transparent blue text</font></string> </resources>
As you can see, the transparency is working for strings declared in strings.xml. For now, I am not sure why it’s working for xml but not for java code. If you know or find an answer please post a comment on the post.
Conclusion
For strings located in strings.xml file use:
textView.setText(getText(R.string.formatted_text));
And for strings in Java code use:
textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));
3. General examples
Other motives that might not keep the style on a text:
Resources:
https://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html