How to Add an WebView to an AlertDialog

public AlertDialog.Builder setView (View view) was added in API level 1
Set a custom view to be the contents of the Dialog.
Parameters: view The view to use as the contents of the Dialog.
Returns: This Builder object to allow for chaining of calls to set methods

You cannot type anything on the WebSite though as the soft keyboard does not display on an textbox on a webage. Also, navigating to other pages or clicking on items on the page may cause errors. It seems good for displaying text of a web page such as a terms of agreement page or FAQ.


1. Create an AlertDialog with Supplied Arguments named myAlertDialogBuilder.

2. In the showAlertDialog method, Instantiate a new WebView named myWebView, after the AlertDialog is instantiated.

3. Add Internet Permission to your Android Project

4. Add the below line to load the URL(website) to load in the WebView.

    myWebView.loadUrl("http://androidsbs.blogspot.com/");

5. Add the below import to the imports section.

import android.webkit.WebViewClient;

6. Add the below code to so it won't ask the user to open another browser.

    myWebView.setWebViewClient(new WebViewClient() { 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

7. Add the below line to link the WebView to the AlertDialog.

myAlertDialogBuilder.setView(myWebView);

8. Compile and run!

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(android.view.View)
http://www.codeofaninja.com/2011/07/android-alertdialog-example.html