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
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
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?
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
.
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.
If you want to add a link and make it clickable,
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
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.
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));
In case if you need it.
Better to use HtmlCompat.fromHtml((htmlString, 0)
for compatibility with older versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With