30

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

description;description;description;description;description;description;description;description;description;description;description;description;description;description;description

Title Two

description;description;description;description;description;description;description;description;description;description;description;description;description;description;description

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);
msg.setText("<html><u>Message</u></html>")

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Title");
ab.setView(msg);
ab.setCancelable(false);

//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?

1
  • For this you don't need to create a new view msg. You can simply use ab.setText(Html.fromHtml( ... )). Also note that the ab methods can be chained. Commented Jun 10, 2015 at 17:47

6 Answers 6

65

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

msg.setText(Html.fromHtml("<u>Message</u>"))

And you also see all HTML tags supported by TextView.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, remember if you pull the message from strings.xml (a good idea in the longer term), you must escape the HTML using CDATA or you won't see any formatting.
Works like wonder!! Thanks.@ρяσѕρєя K please, do you think you could help me with this question stackoverflow.com/questions/25598696/…
If you get a deprecation warning when using Html.fromHtml, look at this SO answer
18

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(R.id.yourHtmlString) rather than getResources().getString(R.id.yourHtmlString), though, or the tags will be completely stripped from the String.

1 Comment

Oh my god. I am developing Android apps for like 6 years now and I didn't know about Resources#getText. I was using Resources#getString all my life. This helps so much. THANKS! +1
9

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

msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);

Comments

5

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",
                    null);
yourAlertDialog.setView(yourCustomWebView);

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

Comments

2

Try this, Font color,

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

Font underline,

   String source = <u>Message</u>

 msg.setText(Html.fromHtml(source));

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.