Android – HTML in textView

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
  • <a href=”…”>
  • <b>
  • <big>
  • <blockquote>
  • <br>
  • <cite>
  • <del>
  • <dfn>
  • <div align=”…”>
  • <em>
  • <font size=”…” color=”…” face=”…”> Size is working only for strings in strings.xml, for strings in Java code it seems that size DOESN’T change. Face is working in both xml and java code but only for default faces that Android supports like (monospace, serif, etc)
  • <h1>
  • <h2>
  • <h3>
  • <h4>
  • <h5>
  • <h6>
  • <i>
  • <img src=”…”>
  • <li>
  • <p>
  • <s>
  • <small>
  • <strike>
  • <strong>
  • <sub>
  • <sup>
  • <tt>
  • <u>

 

  • <abbr>
  • <acronym>
  • <address>
  • <area>
  • <article>
  • <aside>
  • <audio>
  • <base>
  • <basefont>
  • <bdi>
  • <bdo>
  • <body>
  • <button>
  • <canvas>
  • <caption>
  • <center>
  • <code>
  • <col>
  • <colgroup>
  • <data>
  • <datalist>
  • <dd>
  • <div>
  • <dl>
  • <dt>
  • <details>
  • <dialog>
  • <dir>
  • <embed>
  • <fieldset>
  • <figcaption>
  • <figure>
  • <font size…>????? doesn’t work for strings declared in Java code.
  • <footer>
  • <frame>
  • <frameset>
  • <form>
  • <head>
  • <header>
  • <hr>
  • <html>
  • <iframe>
  • <input>
  • <ins>
  • <kbd>
  • <label>

 

  • <legend>
  • <link>
  • <main>
  • <map>
  • <mark>
  • <menu>
  • <menuitem>
  • <meta>
  • <meter>
  • <nav>
  • <noframes>
  • <noscript>
  • <object>
  • <ol>
  • <optgroup>
  • <option>
  • <output>
  • <param>
  • <picture>
  • <pre>
  • <progress>
  • <q>
  • <rp>
  • <rt>
  • <ruby>
  • <samp>
  • <script>
  • <section>
  • <select>
  • <source>
  • <span style=””>
  • <style>
  • <summary>
  • <table>
  • <tbody>
  • <td>
  • <textarea>
  • <tfoot>
  • <th>
  • <thead>
  • <time>
  • <title>
  • <tr>
  • <track>
  • <ul>
  • <var>
  • <video>
  • <wbr>

 

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 CharSequence
  • getString() 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:

  • android:textAllCaps="true"

 

 

 

Resources:

https://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

http://saket.me/html-tags-textview/?utm_source=Android+Weekly&utm_campaign=4fc49af2d9-android-weekly-265&utm_medium=email&utm_term=0_4eb677ad19-4fc49af2d9-337918897