17

I have a hybrid app that uses WebView to render external html from my own site. It had a problem that if any link was clicked, it started a browser window. I found this code to help me out and it works:

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

But now the problem is that I want it to not work for links that have target=_blank in them. So any normal links still open inside the WebView while the links with target=_blank should open in new browser window.

Any way we can do this?

Thanks

mim
  • 477
  • 1
  • 3
  • 13

4 Answers4

39

First,

mWebView.getSettings().setSupportMultipleWindows(true);

Then in WebChromeClient, override OnCreateWindow()

    private class MyWebChromeclient extends WebChromeClient {


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

            WebView newWebView = new WebView(WebViewActivity2.this);
            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) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW);
                    browserIntent.setData(Uri.parse(url));
                    startActivity(browserIntent);
                    return true;
                }
            });
        return true;
    }
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • I am afraid you did not understand it quite well. target=_blank is not something in the url, it is an attribute of the html anchor tag. Something like this: click here I thought it was obvious! – mim Nov 19 '14 at 06:39
  • Changed the code, never worked on HTML, so figured this target thing after little searching. Apologies. – Darpan Nov 19 '14 at 10:51
  • Thanks for the updated code, the new code does not resolve WebViewActivity2. Which package does it belong to? – mim Nov 20 '14 at 07:59
  • This code is for reference, you need to use your own activity name here, or pass context of the app. – Darpan Nov 20 '14 at 09:23
  • The code does not seem to work after I used my own activity name, etc. It did not give error but also did not achieve what I wanted. So I guess I will have to give up this method and simply use a hint that if the url contains a specific string (in my case "redirect"), then I use the old code you gave to initialize a browser intent and cancel this call. I hope it will work. I am accepting your answer for it did help me understand the thing. Thank you. – mim Nov 20 '14 at 12:56
  • I am happy that it gave you idea, but wondering why this one did not work though. If you wish, you could try passing activity's context or put '`getBaseContext()` instead of this whole activityname.this – Darpan Nov 23 '14 at 20:12
  • 1
    instead of WebViewActivity2.this you can just put ```view.getContext()``` – tolbard Jan 19 '18 at 01:08
  • @Darpan It is possible to achieve that without the `Action_View` only to open that url ? – TheCoderGuy Jun 06 '19 at 08:26
  • 1
    @Spritzig ACTION_VIEW is needed so that Android can offer apps that can handle this content-type. In this case, browsers – Darpan Jun 07 '19 at 08:55
  • This works for me. My Html is `` – Brownsoo Han Feb 21 '20 at 06:43
  • 1
    `shouldOverrideUrlLoading` is never called for me here. any suggestions? – ono Apr 12 '23 at 16:14
16

Try this.

myWebView.getSettings().setSupportMultipleWindows(true);
myWebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
    {
        WebView.HitTestResult result = view.getHitTestResult();
        String data = result.getExtra();
        Context context = view.getContext();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
        context.startActivity(browserIntent);
        return false;
    }
});

Reference: Carson Ip

Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
  • This really helped me, it was getHitTestResult().getExtra() I was missing to grab the URL. BIG THANKS! – dev2505 Nov 06 '20 at 18:41
1

Using kotlin

myWebView.settings.javaScriptEnabled = true
myWebView.settings.javaScriptCanOpenWindowsAutomatically = true
0

One more setting required for webview in addition to myWebView.getSettings().setSupportMultipleWindows(true); myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Rushabh Shah
  • 371
  • 1
  • 7
  • 22