Android Quick Tip: Formatting Text with Html.fromHtml()

Android offers you the possibility to easily format text with HTML markup. Thus it’s easy to create text like this:

Sample screen showing some formattings

Sample screen showing some formattings

You probably are going to use bold or italics the most, but there are many more supported.

Here is the list of all supported tags. You can find it in the source of android.text.Html‘s inner class HtmlToSpannedConverter:

Supported HTML-Tags
Tags Format
b, strong Bold
i, em, cite, dfn Italics
u Underline
sub Subtext
sup Supertext
big Big
small Small
tt Monospace
h1 … h6 Headlines
img Image
font Font face and color
blockquote For longer quotes
a Link
div, p Paragraph
br Linefeed

Formatting text from your strings.xml

If you want to support text formatting from within your strings.xml file, you have to escape the tags – or use a CDATA section (thanks to Jose Miguel for pointing this out). Otherwise Android simply ignores them when reading the resource file.

To escape the tags you just need to replace all "<" characters. Luckily you do not have to escape the ">" characters as well. That way the HTML structure is still at least kind of readable. But only kind of.

If you use many HTML tags a CDATA section is better. For the sample above it looks like this:

      <p>Text with markup for <strong>bold</strong>
      and <em>italic</em> text.</p>
      <p>There is also support for a 
      <tt>teletype-style</tt> font. 
      But no use for the <code>code</code>

Even if you can add a lot of HTML tags, you are better off using only minor styling as mixing too much styles makes your text look uneasy instead of being more striking.

The following snippet shows how to use this string from within your Java code:

TextView view = (TextView)findViewById(;
String formattedText = getString(R.string.htmlFormattedText);
Spanned result = Html.fromHtml(formattedText);

Alternatives to consider

For longer texts that use HTML tags, I recommend to use raw files instead.

For more complicated formatting a WebView probably would be better.

Share this article:

Responses are currently closed, but you can trackback from your own site.

26 Responses to “Android Quick Tip: Formatting Text with Html.fromHtml()”

  1. Yuku says:

    You can try using context.getText() function to retrieve styled text directly from resources. So you don’t need to change all the <'s to &lt;'s anymore.

  2. José Miguel says:

    No need to escape the <'s to %lt;'s, just use CDATA!!

    <p>Text with markup for <strong>bold</strong> and <em>italic</em> text.</p>
    <p>There is also support for a teletype-style font. But no use for the <code>code</code> tag!</p>

  3. Anatoly says:


  4. Jason says:

    Thanks much!! Putting together a quick credits page and this works like a champ. J

  5. thank you for solving my problem with this tutorial.

  6. Richard says:

    Hey thanks for the write up on this, helped me out! I had it working with strings I created in my Java code but it was breaking when I swapped it out for a resource string.

    Thanks again!


  7. I am really thankful to blogger. I am really want to learn about android text formatting Thanks its really impressive. with the blog keep sharing.

  8. Noorisys says:

    How to increase size of text

  9. dennis says:

    I tried this, in a emailintent, to get my text in my mail, bold, but it dos not work, do u have any ide what to do ?

    I toke the string, just as it is here on.

    put it in my email

    getString(R.string.htmlFormattedText) ));

    but the bold dos not work.
    I hope you can help me :)


  10. dennis says:

    hmmm do u mean settype text/html?

    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new
    File(Environment.getExternalStorageDirectory() + “/”+ edit.getText().toString() + “.pdf”)));




    • Yes. That’s what I meant.

      BTW: Please use app-specific folders on the sd card. Putting files directly into the root of the sd card tends to clutter it. I’ve written a post about getExternalFilesDir() – if you haven’t read it yet:-)

  11. dennis says:

    ok :) but, it is not working, it understands <p> and <br> but not <b>, hmmm weird.
    Thanks for the info about files, I will try it. (but the app is only for my self, to my work)

  12. dennis says:

    I did change my email app from the standard on samsung tab3 to gmail, and now bold works.

    So thanks for a nice tutorial :)

  13. Farzad says:

    how use ul and li tag in TextView?


  14. vikas says:

    hey guys can someone tell me how can i add loads of text in my application

  15. Hari says:

    I used this to embed image tag in email body when sending html email via Intents, but not working. Any idea, why?
    String body = “this is h1welcome to html“;

    final Intent shareIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(“mailto:”));
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, “The Subject”);
    Html.fromHtml(new StringBuilder()

    • Hari says:

      Looks like my img tag was replaced in the post. Its actually like this(removed html tags)
      String body = this is h1welcome to html;

      • Hari says:

        I’m sorry, still it parsed to html content… :(
        Anyways, my question is adding img tag inside the html content.

Subscribe to RSS Feed My G+-Profile Follow me on Twitter!