34

I did one sample application using WebView, in that web view the URL comes from web services. It's working fine, but if I click any link within that WebView, its automatically go for default web browser. But I want to open within my application web view only. Here my code:

WvBikeSite = (WebView) findViewById(R.id.wv_bikeWebsite);
wvBikeSite.loadUrl(getBundle.getString("www"));
Peter O.
  • 32,158
  • 14
  • 82
  • 96
selva
  • 1,503
  • 3
  • 17
  • 24

12 Answers12

73

You have to set up a webViewClient for your webView.

Sample:

this.mWebView.setWebViewClient(new WebViewClient(){

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
      view.loadUrl(url);
      return true;
    }
});
Terence Lui
  • 2,778
  • 1
  • 18
  • 17
  • this works! but is there any way to allow links that have target=_blank to load in browser instead?? – mim Nov 18 '14 at 12:15
  • 1
    @mim, you can parse the url variable and return false if it contains a certain string – SAS Dec 04 '14 at 19:29
  • `shouldOverrideUrlLoading(WebView view, String url)` is deprecated. See here: https://stackoverflow.com/questions/7308904/link-should-be-open-in-same-web-view-in-android?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – David A May 31 '18 at 22:05
  • This is commentary from the documentation about shouldOverrideUrlLoading method.............This method is not called for requests using the POST "method". *
  • This method is also called for subframes with non-http schemes, thus it is * strongly disadvised to unconditionally call {@link WebView#loadUrl(String)} * with the request's url from inside the method and then return {@code true}, * as this will make WebView to attempt loading a non-http url, and thus fail.
  • – Abhishek Kumar Jun 20 '19 at 06:24
  • This is another paragraph about the same method from documentation........Give the host application a chance to take over the control when a new * url is about to be loaded in the current WebView. If WebViewClient is not * provided, by default WebView will ask Activity Manager to choose the * proper handler for the url. If WebViewClient is provided, return {@code true} * means the host application handles the url, while return {@code false} means the * current WebView handles the url. – Abhishek Kumar Jun 20 '19 at 06:43
  • I've searched through the internet and found that for some people, returning false works while for others returning true works. Beware of Hell. – Abhishek Kumar Jun 20 '19 at 06:44