Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Open target _blank links in WebView with external browser

I build a WebView which displays a website. The website contains links without a target="_blank" attribute and some with it.

I need to open the links with target defined in the external standard device browser and the ones without it inside the WebView.

I'm using a WebViewClient and I tried around with this function but still all my links are opened within the WebView:

Alternative 1:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    return super.shouldOverrideUrlLoading(view, url);        
}

Alternative 2:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    view.loadUrl(url);
    return true;        
}

Does anybody know how I can open blank-links externally?

Thanks!

PS: To avoid missunderstandings: I can't use this approach because the only way I know the link should be opened externallly is the target attribute.

like image 314
Ron Avatar asked Aug 12 '13 13:08

Ron


People also ask

What happens if you add target _blank to a link?

target="_blank" is a special keyword that will open links in a new tab every time. target="blank" will open the first-clicked link in a new tab, but any future links that share target="blank" will open in that same newly-opened tab.

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.

When should I use target _blank?

The most common reason to use `target=”_blank” is so that offsite links open in a separate tab. This allows a user to click on a reference and come back to it later without leaving the current page. It keeps visitors on your site longer and improves most of your metrics: bounce rate, conversion, pages visited.

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.


3 Answers

After visiting the above links, I come up with this code and hope this helps.

wv.getSettings().setSupportMultipleWindows(true);
wv.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;
    }
});
like image 83
Carson Ip Avatar answered Oct 13 '22 23:10

Carson Ip


I faced same problem. I wanted to open my web sites pages inside the application and rest all the pages should be open in Default Browser. I used one technique. If URL contains my website name, then I opened it in WebView and rest all the websites opened in Default browser.

Find Below code, I hope It would be useful for all who faced such problems.

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("/internetgeeks")) {
            browser.loadUrl(url);
            return false;
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}
like image 26
Moinatik Ghori Avatar answered Oct 14 '22 00:10

Moinatik Ghori


I also have the same problem and now I've found the solution.

You also need to use WebChromeClient.You can see this and this . And you can set a WebViewClient to the new WebView and override the shouldOverrideUrlLoading method, then you can get the url and do whatever you want here. If you don't set the WebViewClient, I think it should works too. In my case I want to get the url so I set a WebViewClient to the new WebView.

By the way, if you remove the old webview then when you come back form the browser,the webview is blank. So I retained the webview and added a new one but set the visibility to "gone".

like image 1
ffts Avatar answered Oct 13 '22 23:10

ffts