216

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?

6 Answers 6

356

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; } } 
Sign up to request clarification or add additional context in comments.

4 Comments

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());
@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.
@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.
Can we implement something like: onLongClick, open links in external browser?
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); } } 

1 Comment

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!
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()); 

Comments

17

Add this 2 lines in your code -

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

Comments

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; } }); } 

Comments

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)

1 Comment

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 browser

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.