Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTML in Android Alert Dialog

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?

like image 852
PeakGen Avatar asked Feb 01 '13 18:02

PeakGen


6 Answers

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.

like image 101
ρяσѕρєя K Avatar answered Oct 05 '22 02:10

ρяσѕρєя K


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.

like image 25
alan Avatar answered Oct 05 '22 01:10

alan


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

msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
like image 44
YulCheney Avatar answered Oct 05 '22 01:10

YulCheney


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.

like image 33
keybee Avatar answered Oct 05 '22 01:10

keybee


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));
like image 21
MuraliGanesan Avatar answered Oct 05 '22 01:10

MuraliGanesan


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

like image 42
MetaPlanet Avatar answered Oct 05 '22 01:10

MetaPlanet