Android WebView target=”_blank”

We were having a separate WebView fragment, which was called different urls passed to it.
But a problem with the new versions of Android came up: The WebView couldn’t load links with attribute target=’_blank’. To work around this issue I declared a WebChromeClient opening a new webview with the url from the href.

webSettings.setSupportMultipleWindows(false);

mWebView.setWebChromeClient(new WebChromeClient() {



            @Override

            public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture,
                    Message resultMsg) {
                WebView.HitTestResult result = view.getHitTestResult();
                String data = result.getExtra();
                Log.d("DATA", "" + data);
                // view.loadUrl(data);

                WebView newWebView = new WebView(view.getContext());
                WebView.WebViewTransport transport =
                        (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(view);
                resultMsg.sendToTarget();

                return true;
            }

        });

You may also like...