2

I'm trying to create a AlertDialog that has rich content. When I use the Html.fromHtml() to set the message text like:

AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("title");
adb.setMessage(Html.fromHtml(text));

it only allows me basic HTML elements like <b>(Bold) and <i>(Italic).

When I use a WebView like

WebView webView = new WebView(this);
myWebView.loadData(webContent, "text/html", "utf-8");
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setView(webView);

I lose the default Android style.

What can I do to get Rich context, like <ul> inside an AlertDialog?.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Wiebe Elsinga
  • 386
  • 3
  • 15

3 Answers3

1

Try this, Use custom Adapter

final Dialog custon_dialog = new Dialog(Login.this);
custon_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custon_dialog.setContentView(R.layout.webview);
custon_dialog.setCancelable(true);
// custon_dialog.setTitle(null);

WebView mwebview = (WebView) custon_dialog.findViewById(R.id.webview);
    mwebview.setBackgroundColor(0x00000000);
    mwebview.loadData(webContent, "text/html", "utf-8");
custon_dialog.show();
        }
    });
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
1

You can add the webview inside an activity , and set the activity theme as dialog in menifest file :

<activity .....
          android:theme="@android:style/Theme.Dialog"/>
Anand
  • 2,875
  • 1
  • 18
  • 16
0

Html.fromHtml(text) uses tagsoup

does support simple tags for your reference html.fromhtml

Inspite of using

String text="some html code";

create an html file with your css etc. and place all the files in the assets folder of your application.

Now , in place of this

adb.setMessage(Html.fromHtml(text));  

as you want Rich content displayed in the alerdialog.

This should help you , Remember: you can also inflate the Dialog with a custom layout of your own

AlertDialog.Builder alert = new AlertDialog.Builder(yourclass.this);

   alert.setTitle("title");
   WebView wv = new WebView(yourclass.this);

   wv.loadUrl("file:///assets/yourHtmlFileName.html");

                        wv.setWebViewClient(new WebViewClient()
                        {
                            public boolean shouldOverrideUrlLoading(WebView view, String url)
                            {
                                view.loadUrl(url);

                                return true;
                            }
                        });

                        alert.setView(wv);

                        alert.show();

                    }
                });
Prateek
  • 3,923
  • 6
  • 41
  • 79
  • So I'm bound to the CSS? and can't use Holo.Theme?. – Wiebe Elsinga Feb 21 '13 at 10:55
  • Remember: you can also inflate the Dialog with a custom layout of your own . With this line I want to state that in your layout.xml you can use `@android:style/Theme.Holo` as `android:background` in layout.xml's root element. – Prateek Feb 21 '13 at 11:00