16

I have webview in my application and I want it to open pop up windows when clicking on a link inside webview. I have added following code but it didn't work:-

WebSettings webSettings = webViewPage.getSettings();    
webSettings.setJavaScriptEnabled(true);    
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

That's how I want pop up window to appear the popup should appear like this

frogatto
  • 28,539
  • 11
  • 83
  • 129
Vikalp
  • 2,051
  • 4
  • 18
  • 24

4 Answers4

53

I am answering my own question after 3 long years:

When a link is touched inside a webpage then depending on a webpage implementation there are two possible scenarios: 1) Link will be opened in same window. 2) Link will be opened in new window.

Well Its easy to handle 1st scenario using below code:

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

Overiding shouldOverrideUrlLoading inside WebViewClient implementation will open link in same window.

Now lets look at the 2nd case, where webpage is requesting a url to be open in new window. For this case we need to tell our webview to support multiple windows like below:

webView.getSettings().setSupportMultipleWindows(true);

and then adding a new web chrome client to webview to get event when new window is requested by webpage

webView.setWebChromeClient(new WebChromeClient() {


        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog,
                boolean isUserGesture, Message resultMsg) {



                WebView newWebView = new WebView(WebpageActivity.this);
                newWebView.getSettings().setJavaScriptEnabled(true);
                newWebView.getSettings().setSupportZoom(true);
                newWebView.getSettings().setBuiltInZoomControls(true);
                newWebView.getSettings().setPluginState(PluginState.ON);
                newWebView.getSettings().setSupportMultipleWindows(true);
                view.addView(newWebView);
                WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(newWebView);
                resultMsg.sendToTarget();

                newWebView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                });

                return true;
            }
        }

    });

Cheers!!!

Vikalp
  • 2,051
  • 4
  • 18
  • 24
  • 2
    That saved my day @Vikalp. Thank you so much for the solution. I had the problem in loading a popup of a site in webview which was causing a crash in app. WebviewTransport concept worked on that. Thanks again! – Swr7der Sep 28 '17 at 10:32
  • 1
    @Swr7der Cheers! – Vikalp Sep 29 '17 at 08:41
  • 1
    This is a great answer. I didn't think to cast `resultMsg.obj` to `WebView.WebViewTransport` and that kept me from arriving at this. The documentation [here](https://developer.android.com/reference/android/webkit/WebChromeClient.html) should be made clearer. – davejoem Nov 17 '17 at 10:48
  • 2
    Talking about second scenario - the popup thus opened occupies only a smaller portion of the screen. Is there a way to make it full screen or change its size so that it's not ignored by the user in cases when its not visible to the user because of its smaller size? – Amar Jul 20 '20 at 01:13
  • to handle 1st scenario `return false` will enough – Mohd Qasim Dec 30 '20 at 08:14
  • 1
    before implementing this read the caution of the implementation here __https://developer.android.com/guide/webapps/webview#web-management__ of `onCreateWindow` – Mohd Qasim Jan 25 '21 at 13:10
  • https://stackoverflow.com/questions/66068011/razor-pay-failed-inapp-browser-in-android any solution on this @Vikalp . – Jithish P N Feb 08 '21 at 12:09
  • Thanks a ton @Vikalp dude!! – Saket89 Aug 27 '21 at 19:56
1

Add this line to your web view web.addJavascriptInterface(new Jscalls(this), "Android");

Then add this line to your href in html that is loaded in the web view href="javascript:showAndroidToast('Data to be shown or the URL from which data is to be shown')"

   public class Jscalls {
    Context mContext;

    Jscalls(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */ 
    @JavascriptInterface
            public void showToast(final String toast) {
                // Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
                if (NetworkConnection.isConnected(mContext)) {
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        public void run() {
                            dialog = new Dialog(mContext);
                            dialog.setTitle("Title");

                            dialog.setContentView(R.layout.dialog);
                            dialog.setCancelable(true);
                            // there are a lot of settings, for dialog, check them all out!
                            // set up text

                            WebView web = (WebView) dialog.findViewById(R.id.webVie);
                            web.getSettings().setJavaScriptEnabled(true);
                            /*web.clearHistory();
                            web.clearFormData();
                            web.clearCache(true);*/
                            web.setWebViewClient(new HelloWebViewClient());
                            web.setOnLongClickListener(new OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                    return true;
                                }
                                });
                            web.setLongClickable(false);
                            try{
                                web.loadUrl(Url.mainUrl 
                                        + toast);//Url to load data from in pop-up
                            }catch (Exception e) {
                                // TODO: handle exception
                                //e.printStackTrace();
                            }   


                            // dialog.setContentView(web);
                            dialog.show();
                        }
                    });

                    //web = null ;
                    // now that the dialog is set up, it's time to show it

                    /*
                     * dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                     * R.drawable.ic_launcher);
                     */

                    // dialog.setFeatureDrawable
                } else {
                    LoginMainActivity.validateEmail("Alert!!",
                            "This feature requires wi-fi or internet connection.",
                            mContext);
                }
            }
            private class HelloWebViewClient extends WebViewClient {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            }
            Dialog dialog;
        }
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
  • 1
    I dont want to open popup for html pages made by me but If I click on a link in some websites page and that link opens a new window then I also want to open that page in new window.. – Vikalp Apr 26 '14 at 15:20
  • @Vikalp: Are you able to solve the popup issue? I am unable to open the popup in WebView! If anybody solved to display the popup in WebView, please post the solution. – Rajeev Sahu Jan 23 '17 at 09:28
  • @user1182217: After 3 long years, you made me remind my those days. Those were the real days man. Well leave all these and I have answered my own questions. – Vikalp Jan 23 '17 at 18:47
0

If someone is facing issue like pop is not opening or there is black screen when you are trying to open js pop up in webview. Please try to change height and width of webview like:-

android:layout_width="fill_parent"
android:layout_height="fill_parent"

OR

 android:layout_width="match_parent"
android:layout_height="match_parent"

like this:-

<WebView
 android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
pankaj
  • 29
  • 1
0

only change

android:layout_width="match_parent"
android:layout_height="match_parent"

to

android:layout_width="fill_parent"
android:layout_height="fill_parent"

all popups will appear ....

RusArtM
  • 1,116
  • 3
  • 15
  • 22
Mustafa
  • 1
  • 2