WebView does not display popup


I had a scenario where I need to display an url in Webpage .
The url contains a popup which does not show and an empty screen is displayed instead of popup.
plz find below my implementation:

     webView = (WebView) instamedWebView.findViewById(R.id.webView);<br>
     WebSettings settings = webView.getSettings();
    settings.setBuiltInZoomControls(true);
    settings.setUseWideViewPort(true);
    settings.setJavaScriptEnabled(true);
    settings.setSupportMultipleWindows(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setLoadsImagesAutomatically(true);
    settings.setDomStorageEnabled(true);
    settings.setLoadWithOverviewMode(true);
     settings.setAllowFileAccess(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);

try {
    String url = "https://ppay-dev.com/Payment/abc.aspx";
    String postData = String postData = "amount=" + URLEncoder.encode(userName, "UTF-8") + "&FirstName=" + URLEncoder.encode(password, "UTF-8");
        webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {

            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {


            }
        });
        webView.postUrl(url,postData.getBytes());
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
        });
   catch (Exception e){
        e.printStackTrace();
    }


Can anyone suggest me how to display popups in Android WebView

Upvotes: 0

Views: 4676

Answers (3)

Myth
Myth

Reputation: 1258

Instead of overriding onJsAlert, just use:

WebView wv = new WebView(this); 
wv.setWebChromeClient(new WebChromeClient());

Upvotes: 0

K.Sopheak
K.Sopheak

Reputation: 23144

It is said to use WebChromeClient and Override method onJsAlert(). Here is a sample code.

WebView wv=new WebView(this);   
wv.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
                //Required functionality here
                return super.onJsAlert(view, url, message, result);
       }

This is link for more info.

Upvotes: 1

ZeroOne
ZeroOne

Reputation: 9117

Maybe you can try to override web alert to native alert. It works for me.

webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.i("TITLE", title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }

            // OVERRIDE WEB CONFIRM ALERT TO NATIVE ALERT
            @Override
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                new AlertDialog.Builder(VideoTutorial.this)
                        .setTitle("DOMINOS SAYS:")
                        .setMessage(message)
                        .setPositiveButton(android.R.string.ok, (dialog, which) -> result.confirm())
                        .setNegativeButton(android.R.string.cancel, (dialog, which) -> result.cancel())
                        .create().show();

                return true;
            }

            // OVERRIDE WEB ALERT TO NATIVE ALERT
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {

                new AlertDialog.Builder(VideoTutorial.this)
                            .setTitle("DOMINOS SAYS:")
                            .setMessage(message)
                            .setPositiveButton(android.R.string.ok,
                                    (dialog, which) -> result.confirm())
                            .create().show();
                return true;
            }

            // OVERRIDE WEB PROMPT ALERT TO NATIVE ALERT PROMPT
            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
                new MaterialDialog.Builder(VideoTutorial.this)
                        .title("DOMINOS SAYS:")
                        .content(message)
                        .inputType(InputType.TYPE_CLASS_TEXT)
                        .inputRangeRes(2, 20, R.color.dominoRed)
                        .input("No 1 Pizza Fav", "", (dialog, input) -> result.confirm(input + ""))
                        .positiveText("OK")
                        .negativeText("CANCEL")
                        .onNegative((dialog, which) -> result.cancel())
                        .show();

                return true;
            }
        });

Upvotes: 1

Related Questions