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?

Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50
JaVadid
  • 7,107
  • 13
  • 42
  • 54

6 Answers6

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;
    }
}
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
David Webb
  • 190,537
  • 57
  • 313
  • 299
  • 97
    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());` – Adam Oct 07 '12 at 20:25
  • 7
    @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 ) it will navigate your app's main frame to that URL most likely breaking the app as a side effect. – marcin.kosiba Feb 07 '14 at 12:35
  • 1
    @Adam ++ - 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. – Peter Ajtai Feb 15 '14 at 00:28
  • Can we implement something like: onLongClick, open links in external browser? – TheOnlyAnil Apr 16 '15 at 11:03
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);
    }           
}
realgt
  • 1,715
  • 15
  • 12
  • 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! – Brigante Aug 23 '11 at 10:20
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());
abbas
  • 6,453
  • 2
  • 40
  • 36
17

Add this 2 lines in your code -

mWebView.setWebChromeClient(new WebChromeClient()); 
mWebView.setWebViewClient(new WebViewClient());
Confuse
  • 5,646
  • 7
  • 36
  • 58
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;
        }
    });
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
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)