Sameer Z. Sameer Z. - 2 months ago 11
Android Question

Android:How to add support the javascript alert box in WebViewClient?

Hi
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 WebChromeClient then i can not do other things.Any one know how mix the both future?

I have check the code for javasript alert box at http://lexandera.com/2009/01/adding-alert-support-to-a-webview/



Thank you

Answer

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 = new ProgressDialog(this);
progressBar.setCancelable(true);
progressBar.setMessage("Loading...");
progressBar.show();

final Context mapp = this;

webView.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Log.i("TEST", "Processing webview url click...");
    // to kill activity
    view.loadUrl(url);
    return true;
}

public void onPageFinished(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(new
 WebChromeClient() {            
 @Override
 public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
    new AlertDialog.Builder(mapp)
        .setTitle(R.string.title_dialog_alert)
        .setMessage(message)
        .setPositiveButton(android.R.string.ok,
            new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    result.confirm();
                }
            }).setCancelable(false).create().show();

        return true;
 }

 @Override
 public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        new AlertDialog.Builder(mapp)
        .setTitle(R.string.title_dialog_confirm)
        .setMessage(message)
        .setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                result.confirm();
            }
        }).setNegativeButton(android.R.string.cancel, 
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                result.cancel();
            }
        }).create().show();
    return true;
}

 @Override
     public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
          final LayoutInflater factory = LayoutInflater.from(mapp);
          final View v = 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);

           new AlertDialog.Builder(mapp)
                .setTitle(R.string.title_dialog_prompt)
                .setView(v)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                               String value = ((EditText)v.findViewById(R.id.prompt_input_field)).getText().toString();
                               result.confirm(value);
                         }
            })
            .setNegativeButton(android.R.string.cancel,
                   new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int whichButton) {
                               result.cancel();
                         }
             })
             .setOnCancelListener(
                   new DialogInterface.OnCancelListener() {
                         public void onCancel(DialogInterface dialog) {
                               result.cancel();
                         }
             })
             .show();

             return true;
        };

 });

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

Comments