29

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?

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 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. –  Jun 10 '15 at 17:47

6 Answers6

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.

Sufian
  • 6,405
  • 16
  • 66
  • 120
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 10
    Also, remember if you pull the message from `strings.xml` (a good idea in the longer term), you must [escape the HTML using CDATA](http://stackoverflow.com/questions/13425002/android-html-in-strings-xml) or you won't see any formatting. – declension Dec 16 '13 at 21:36
  • Works like wonder!! Thanks.@ρяσѕρєя K please, do you think you could help me with this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 01 '14 at 23:02
  • 1
    If you get a deprecation warning when using Html.fromHtml, look at [this SO answer](https://stackoverflow.com/a/37905107/183791) – dusan Mar 02 '19 at 17:51
17

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.

alan
  • 171
  • 1
  • 4
  • 3
    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 – flxapps May 26 '16 at 11:11
9

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

msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
YulCheney
  • 2,877
  • 2
  • 18
  • 14
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.

keybee
  • 1,498
  • 20
  • 32
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));
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
1

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

MetaPlanet
  • 129
  • 4