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

Note: 80 from¬†#800000FF should have added a blue color with an alpha of 50% to the “blue” text

Examples that work

  • Using rgb

  • Using hexa (it works with lower case letters also)

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.


Examples that¬†DON’T¬†work

  • using getString()

  • using getString() + Html.fromHtml()

 Examples that work

  • using getText()

  • using the resource id directly

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

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

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.





For strings located in strings.xml file use:


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:






Add links to a TextView in Android

Maybe you need to add links to a TextView on Android but you don’t want to use Linkify¬†for some reasons and you also want that link to be opened when the user taps it.

Here is a simple solution:

Note that I used the fromHtml() method from the Html class to generate a Spanned object that maps the given Html string into something that the TextView will know how to display.

You can use that to add other Html styles to your text when you display it in a TextView but you need to make sure you know the supported tags.

I extracted the supported tags from the source code ad you can find those below.

Supported HTML tags by the Html Android class:

– br
– p
– div
– em
– b
– strong
– cite
– dfn
– i
– big
– small
– font
– blockquote
– tt
– a
– u
– sup
– sub
– h (1 to 6)
– img

Source here.