Clicking URLs opens default browser
Asked Answered
M

6

215

I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView. But it's opening the default browser and loading the page there?

I have enabled JavaScript. But still it's not working. Have I forgotten something?

Monocyte answered 4/3, 2010 at 11:22 Comment(0)
S
355

If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.

You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.

You set the WebViewClient of your WebView using the setWebViewClient() method.

If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
Serviceman answered 4/3, 2010 at 12:3 Comment(4)
It looks like this is the default behaviour for WebViewClient and doesn't need subclassing if this is all you're doing. I got this to work just by doing myWebView.setWebViewClient(new WebViewClient());Runkle
@dave-webb please update the sample to not call loadUrl. Everyone reading this - please don't replicate the code (return false from the callback instead of calling view.loadUrl). Calling loadUrl introduces a subtle bug where if you have any iframe within the page with a custom scheme URL (say <iframe src="tel:123"/>) it will navigate your app's main frame to that URL most likely breaking the app as a side effect.Returnable
@Runkle ++ - yes I noticed this too - I couldn't understand why one of my web views was NOT using the system browser, and it was because I had added a new webclient to it that was only overriding ANOTHER method. That took me a while to debug.Fated
Can we implement something like: onLongClick, open links in external browser?Wil
T
54

in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:

@Override
public void onLoadResource(WebView view, String url)
{
    if (url.equals("http://redirectexample.com"))
    {
        //do your own thing here
    }
    else
    {
        super.onLoadResource(view, url);
    }           
}
Tutuila answered 22/4, 2010 at 14:53 Comment(1)
This is great! Saved me many hours of trying to work out how to play an MP3 file from a link in a webview. Thanks a lot realgt!Rattray
A
26

Official documentation says, click on a link in a WebView will launch application that handles URLs. You need to override this default behavior

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

or if there is no conditional logic in the method simply do this

myWebView.setWebViewClient(new WebViewClient());
Alphonsa answered 20/9, 2013 at 16:51 Comment(0)
S
17

Add this 2 lines in your code -

mWebView.setWebChromeClient(new WebChromeClient()); 
mWebView.setWebViewClient(new WebViewClient());
Sentimentalism answered 3/10, 2014 at 9:39 Comment(0)
A
9

The method boolean shouldOverrideUrlLoading(WebView view, String url) was deprecated in API 24. If you are supporting new devices you should use boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).

You can use both by doing something like this:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    newsItem.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }
    });
} else {
    newsItem.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
}
Aerate answered 22/6, 2016 at 19:7 Comment(0)
D
8

Arulx Z's answer was exactly what I was looking for.

I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:

mWebView.setWebChromeClient(new WebChromeClient()); mWebView.setWebViewClient(new WebViewClient());

exactly under your WebView statement.

Here's a example of my implemented WebView code:

public class WebView1 extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
    wv.setWebViewClient(new WebViewClient());    //the lines of code added
    wv.setWebChromeClient(new WebChromeClient()); //same as above

    wv.loadUrl("http://www.google.com");
}}

this way, every link clicked in the website will load inside your WebView. (Using Android Studio 1.2.2 with all SDK's updated)

Delegacy answered 15/6, 2015 at 10:45 Comment(2)
This is the most simple way to do it. Thanks SamRansdell
What if I dont want to open it in the same webview? Im using webview and currently the hyperlinks within the webview opens in the same webview, I want to open in external browserBrass

© 2022 - 2024 — McMap. All rights reserved.