• Thursday, February 11, 2016

    Android - How to show javascript alert in WebView


    How to show javascript alert in WebView?

    In android, WebView does not show javascript alert dialog in activity. Here I give a solution that can easily handle the alert dialog with user customization. 

    Write the following code under activity after initiating the WebView.


    WebView wv= (WebView) findViewById(R.id.webView);
    final Context mc=this;



    wv.setWebChromeClient(new WebChromeClient() {
        @Override    
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            new AlertDialog.Builder(mc)
                    .setTitle("masoomyf")
                    .setMessage(message)
                    .setPositiveButton("Button Text", new AlertDialog.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            result.confirm();
                        }
                    })
                    .setCancelable(false)
                    .create().show();
            return true;
        }
    });
    wv.getSettings().setJavaScriptEnabled(true);
     

    If there is any error after implementing this code and let us know. 

    KeyWords: Android WebView JavaScript Alert

    Syed Masoom Rizvi

    Author & Editor

    Hi, This is Syed Masoom Rizvi, who is continue exploring the world of programming ,programming and programming... :)

    0 comments:

    Post a Comment