Skip to content Skip to sidebar Skip to footer

Android:how To Add Support The Javascript Alert Box In Webviewclient?

I implement the many things using the webViewClient like onUnhandledKeyEvent,shouldOverrideUrlLoading and more.If want to add the support for alertbox then need to switch to WebChr

Solution 1:

Implement the WebViewClient and WebChromeClient both like this

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

progressBar = newProgressDialog(this);
progressBar.setCancelable(true);
progressBar.setMessage("Loading...");
progressBar.show();

final Context mapp = this;

webView.setWebViewClient(newWebViewClient() {

publicbooleanshouldOverrideUrlLoading(WebView view, String url) {
    Log.i("TEST", "Processing webview url click...");
    // to kill activity
    view.loadUrl(url);
    returntrue;
}

publicvoidonPageFinished(WebView view, String url) {
    Log.i("TEST", "Finished loading URL: " + url);
    if (progressBar.isShowing()) {
         progressBar.dismiss();
    }
}........

then implement the WebChromeClient for javascript alert,confirm and prompt

 webView.setWebChromeClient(newWebChromeClient() {            
 @OverridepublicbooleanonJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
    newAlertDialog.Builder(mapp)
        .setTitle(R.string.title_dialog_alert)
        .setMessage(message)
        .setPositiveButton(android.R.string.ok,
            newAlertDialog.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int which) {
                    result.confirm();
                }
            }).setCancelable(false).create().show();

        returntrue;
 }

 @OverridepublicbooleanonJsConfirm(WebView view, String url, String message, final JsResult result) {
        newAlertDialog.Builder(mapp)
        .setTitle(R.string.title_dialog_confirm)
        .setMessage(message)
        .setPositiveButton(android.R.string.ok,
            newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int which) {
                result.confirm();
            }
        }).setNegativeButton(android.R.string.cancel, 
        newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int which) {
                result.cancel();
            }
        }).create().show();
    returntrue;
}

 @OverridepublicbooleanonJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
          finalLayoutInflaterfactory= LayoutInflater.from(mapp);
          finalViewv= factory.inflate(R.layout.javascript_prompt_dialog, null);

          ((TextView)v.findViewById(R.id.prompt_message_text)).setText(message);
          ((EditText)v.findViewById(R.id.prompt_input_field)).setText(defaultValue);

           newAlertDialog.Builder(mapp)
                .setTitle(R.string.title_dialog_prompt)
                .setView(v)
                .setPositiveButton(android.R.string.ok,
                        newDialogInterface.OnClickListener() {
                            publicvoidonClick(DialogInterface dialog, int whichButton) {
                               Stringvalue= ((EditText)v.findViewById(R.id.prompt_input_field)).getText().toString();
                               result.confirm(value);
                         }
            })
            .setNegativeButton(android.R.string.cancel,
                   newDialogInterface.OnClickListener() {
                         publicvoidonClick(DialogInterface dialog, int whichButton) {
                               result.cancel();
                         }
             })
             .setOnCancelListener(
                   newDialogInterface.OnCancelListener() {
                         publicvoidonCancel(DialogInterface dialog) {
                               result.cancel();
                         }
             })
             .show();

             returntrue;
        };

 });

for more details please check out http://code.google.com/p/mosembro/source/browse/trunk/src/com/lexandera/mosembro/Mosembro.java

Solution 2:

If want to add the support for alertbox then need to switch to WebChromeClient then i can not do other things.

You can have more than one object in most programming languages, including Java. Hence, you have both a WebViewClientand a WebChromeClient as separate objects.

Solution 3:

You need to set WebChromeClient(This handles JavaScript dialogs, favicons, titles, and the progress) for your WebView .

WebView wView = newWebView(this){
            @OverridepublicbooleanonJsAlert(WebView view, String url, String message,
                    JsResult result) {
                // TODO Auto-generated method stubLog.i("my log","Alert box popped");
                returnsuper.onJsAlert(view, url, message, result);
            }
};
setContentView(wView);

wView.getSettings().setJavaScriptEnabled(true);

WebChromeClient cClient = newWebChromeClient();
wView.setWebChromeClient(cClient);

wView.loadUrl("file:///android_asset/" + yourHtmlFile);

Hope it helps :)

Post a Comment for "Android:how To Add Support The Javascript Alert Box In Webviewclient?"