5

I am trying to allow a new window to be opened in a WebView when a button on a website is pressed. I have tried enabling both setJavaScriptCanOpenWindowsAutomatically and setSupportMultipleWindows, but still nothing happens when the button is pressed.

public class WebView extends AppCompatActivity {

    android.webkit.WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        myWebView = (android.webkit.WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setSupportMultipleWindows(true);
        myWebView.loadUrl("https://somewebsite.com/");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState ) {
        super.onSaveInstanceState(outState);
        myWebView.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        myWebView.restoreState(savedInstanceState);
    }

}

I have read and tried the suggested answers in the following:

I think the reason the answer in the question linked here may not be working for my case because the linked question is talking about a popup within the same window, but the button I am clicking opens a new window entirely (which I cannot change).

Does anyone why know why setJavaScriptCanOpenWindowsAutomatically and setSupportMultipleWindows aren't doing the trick and how I can set up my webview so that it allows clicking a button that opens a new window?

Rbar
  • 3,740
  • 9
  • 39
  • 69
  • keep `window.open()` in your scripting side code. and override `onCreateWindow` in `WebChromeClient` in android side – Mohd Qasim Jan 19 '21 at 07:35

1 Answers1

0

You are on the right path, we had the same issue. setJavaScriptCanOpenWindowsAutomatically and setSupportMultipleWindows must be set on the Chrome Browser. Your code is using the default webview:

myWebView = (android.webkit.WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();

The webview must be extended to use chrome setWebChromeClient:

myWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog,
                    boolean isUserGesture, Message resultMsg) {

                    // Add your settings to the chrome browser:
                    WebView newWebView = new WebView(WebpageActivity.this);
                    newWebView.getSettings().setJavaScriptEnabled(true);
                    newWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                    newWebView.getSettings().setSupportMultipleWindows(true);
           }
});
Jason
  • 2,555
  • 2
  • 16
  • 17