13

It wasn't showing JSAlert on any devices. And then I set a WebChromeClient:

webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                    JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message,
                    String defaultValue, JsPromptResult result) {
                return super.onJsPrompt(view, url, message, defaultValue,
                        result);
            }

        });

Now it works fine on all pre-Lollipop devices. And doesn't show any alert on Lollipop devices.

Logcat says "Cannot create a dialog, the WebView context is not an Activity".

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
  • See [Showing an AlertDialog from a Webview outside of an Activity](http://stackoverflow.com/questions/26044179/showing-an-alertdialog-from-a-webview-outside-of-an-activity) post probably help – ρяσѕρєя K Apr 09 '15 at 05:54
  • @ρяσѕρєяK I ve already come across this. It didn't help. Gives the same same result. I actually put breakpoints in onJSAlert method to check if it's actually called. And it didn't. – Seshu Vinay Apr 09 '15 at 06:01
  • @SeshuVinay Does the debugger pass through the line `return super.onJsPrompt(view, url, message, defaultValue, result);` ? If yes, remove the super invocation and put a custom Dialog using the context of your outer activity. – bonnyz Apr 13 '15 at 16:40
  • Have you fixed this already? – Bojan Kseneman Apr 16 '15 at 16:54
  • @BojanKseneman No. Is there a fix? – Seshu Vinay Apr 17 '15 at 05:08

3 Answers3

4

There is a bug introduced in version 40 of WebView, and is fixed in version 42.

Check the issue reported at

https://code.google.com/p/chromium/issues/detail?id=478204

Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49
0

It needs to require a permission first

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

If you are using Jscript for opening in webview. You can create Dialog like this write this code in your jscript

<input type="button" value="Display dialog" onClick="displayDialog()" />
    <script>
        function displayDialog() {
            //alert("Javascript Dialog");
            Android.showDialog();
        }
    </script>
</input>

OR a very simple way to create a dialog in webview

webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            mResult = result;
            AlertDialog dialog = new AlertDialog.Builder(MyService.this)
                    .setTitle("test Dialog")
                    .setMessage("This is test dialog")
                    .setOnCancelListener(new CancelListener())
                    .setNegativeButton("Cancel", new CancelListener())
                    .setPositiveButton("Ok", new PositiveListener())
                    .create();
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            dialog.show();

            return true;
        }
    });

One last thing if these above doesn't work You need to create custom dialog in Layout pro-grammatically and add your view in this. Like

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT, 
                WindowManager.LayoutParams.WRAP_CONTENT
                WindowManager.LayoutParams.TYPE_PHONE,
                0,
                PixelFormat.TRANSLUCENT);

params.gravity = Gravity.CENTER;

windowManager.addView(yourView, params);
Ahmad Arslan
  • 4,498
  • 8
  • 38
  • 59
0

Please share the code that you are using to construct the WebView. It sounds like you are constructing it with a Service or Application Context rather than an Activity.

Please try using an Activity context.

ksasq
  • 4,424
  • 2
  • 18
  • 10
  • 1
    https://code.google.com/p/chromium/issues/detail?id=447607 i am using activity context only , this issue exists only in the latest update in android system webview, i am searching solution for this issue. – Tamilselvan Kalimuthu Apr 14 '15 at 13:45
  • I second @TamilselvanKalimuthu. Issue is with prelollipop devices. seems to be a bug in the latest android. – Seshu Vinay Apr 15 '15 at 09:23
  • I see, thanks for the clarification. Can you try with the latest beta build of WebView in the Google Play Store? For details please see https://plus.google.com/u/0/communities/105434725573080290360 – ksasq Apr 17 '15 at 16:02