Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView links to same window with target=_blank to open new window

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

like image 337
mim Avatar asked Nov 19 '14 05:11

mim


People also ask

How do I open a new tab in WebView?

Android WebView links to same window with target=_blank to open new window. New!

How do I open a link in WebView?

Within the shouldOverrideUrlLoading() method simply get the URL from the request and pass into the Intent. See the full example.

How do I open popups in WebView?

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

What is a WebView link?

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.


1 Answers

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

like image 77
Aashish Avatar answered Oct 20 '22 23:10

Aashish