java – Using HTML in Android Alert Dialog-ThrowExceptions

Exception or error:

I have some amount of informations to be displayed in Dialog Box. It comes like Title, then under it text; Title, then under it text. Like wise, there are 4 titles and 4 descriptions to be displayed. It should come like this

Title One


Title Two


As you can see, there are bold texts, underlined texts, line breaks etc. I want to add this kind of a text to the alert box, so below is what I tried.

TextView msg = new TextView(this);

AlertDialog.Builder ab = new AlertDialog.Builder(this);

//rest of the code

However this trick didn’t work. What happened is, all the HTML tags showed up as they are! And the text is not clear! Seems like it mixed with the background of the default colour of AlertBox, black. How can I solve this issue? Please help!

PS: Or am I using the wrong method? Wrong dialog box?

How to solve:

You will need to use Html.fromHtml() to use HTML tags in TextView as:


And you also see all HTML tags supported by TextView.


As it turns out, you don’t actually need any extra TextViews to do this. Simply include the HTML in your alert’s “setMessage()” call (which replaces the “setView()” call in your question) and pass it the html-formatted string. Be sure to only use <b>, <u>, and <i> in your formatting, though because those are the only tags it supports. If you’re using a String resource for the text in your alert, call getResources().getText( rather than getResources().getString(, though, or the tags will be completely stripped from the String.


If you want to add a link and make it clickable,



If you need to add more complex HTML, with CSS and META, you can add a WebView to the dialog, like this:

String webViewString = yourMeta + yourCss + yourHtml;
yourCustomWebView.loadData(webViewString, "text/html; charset=UTF-8",

This way, you can display fully formatted HTML pages in your dialog.


Try this,
Font color,

   String source = "<b><font color=#ff0000> Loading. Please wait..."
                + "</font></b>";

Font underline,

   String source = <u>Message</u>



In case if you need it.
Better to use HtmlCompat.fromHtml((htmlString, 0) for compatibility with older versions.

Leave a Reply

Your email address will not be published. Required fields are marked *