Full width home advertisement

Post Page Advertisement [Top]


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

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib