Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a webview confirm dialog?

Tags:

I'm displaying a webpage in a WebView and on the webpage, there is a button. When you click the button, a confirmation dialog is supposed to popup, but it doesn't show in my WebView. It does popup if I go to the same webpage in the android browser. Anyone know how to handle popup dialogs coming from a webpage inside your WebView?

like image 968
brockoli Avatar asked Apr 28 '10 02:04

brockoli


People also ask

What is confirm() in html?

confirm() instructs the browser to display a dialog with an optional message, and to wait until the user either confirms or cancels the dialog.

How do I open popups in WebView?

Overiding shouldOverrideUrlLoading inside WebViewClient implementation will open link in same window. webView. setWebChromeClient(new WebChromeClient() { @Override public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) { WebView newWebView = new WebView(WebpageActivity.

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

What is confirm in javascript?

Javascript | Window confirm() Method The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed. Syntax: confirm(message)


2 Answers

Ok, found the answer and here it is!

In order to handle a popup confirmation coming from a webpage in your WebView, you need to override the onJsConfirm method in WebChromeClient to display the popup as an Android Alert dialog. Here is the code to do so.

final Context myApp = this;  final class MyWebChromeClient extends WebChromeClient {     @Override     public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {         new AlertDialog.Builder(myApp)         .setTitle("App Titler")         .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;     } } 

Don't forget to set your WebChromeClient in your WebView...

    mWebView.setWebChromeClient(new MyWebChromeClient()); 

Note.. this isn't my code, but I found it and it works perfectly for handling javascript confirmation dialogs in a WebView!

Cheers!

like image 79
brockoli Avatar answered Sep 29 '22 22:09

brockoli


Thanks Brockoli for the method. I needed this for Xamarin.Android

 public class MyWebChromeClient : WebChromeClient {     private Context mContext;     private JsResult res;      public MyWebChromeClient(Context context)     {         mContext = context;     }        public override bool OnJsConfirm(WebView view, string url, string message, JsResult result)     {          res = result;          AlertDialog.Builder builder = new AlertDialog.Builder(mContext);         builder.SetTitle("Confirm:");         builder.SetMessage(message);         builder.SetPositiveButton(Android.Resource.String.Ok,  OkAction);         builder.SetNegativeButton(Android.Resource.String.Cancel, CancelAction);         builder.Create();         builder.Show();          return true;           //return base.OnJsConfirm(view, url, message, result);     }      private void CancelAction(object sender, DialogClickEventArgs e)     {         res.Cancel();     }      private void OkAction(object sender, DialogClickEventArgs e)     {         res.Confirm();     } } 

This back in the activity where webview is created (web_view)

web_view.SetWebChromeClient(new MyWebChromeClient(this)); 
like image 22
Pmc Avatar answered Sep 29 '22 22:09

Pmc