5

I have WebView in which I want to open links belong to domain www.example.org in webview while all other links (if clicked) open by the default browser outside of my application.

I tried to use public boolean shouldOverrideUrlLoading(WebView view, String url) but it does not work properly.

Here is the code that does not work:

public class MyWebViewClient extends WebViewClient {
    @Override
               public boolean shouldOverrideUrlLoading(WebView view, String url) {
                   try {
                   URL urlObj = new URL(url);
                   if (urlObj.getHost().equals("192.168.1.34")) {
                       view.loadUrl(url);
                       return true;
                   } else {
                       view.loadUrl(url);
                       return false;
                     }
                   } catch (Exception e) {

                   }
               }
}

In both cases ( return true and return false) the URL is handled by my application.

ace
  • 11,526
  • 39
  • 113
  • 193
  • 1
    This code really gives you no clue what the different behaviors are when returning a different boolean value because you are calling view.loadUrl() in both cases, thus producing the same result. If you removed that line from both statements you would see that return false still loads the url in the WebView...and return true does nothing (you must manually do something). – devunwired Feb 07 '11 at 14:32

3 Answers3

24

Once you create and attach a WebViewClient to your WebView, you have overridden the default behavior where Android will allow the ActivityManager to pass the URL to the browser (this only occurs when no client is set on the view), see the docs on the method for more.

Once you have attached a WebViewClient, returning false form shouldOverrideUrlLoading() passes the url to the WebView, while returning true tells the WebView to do nothing...because your application will take care of it. Unfortunately, neither of those paths leads to letting Android pass the URL to the browser. Something like this should solve your issue:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    try {
      URL urlObj = new URL(url);
      if( TextUtils.equals(urlObj.getHost(),"192.168.1.34") ) {
        //Allow the WebView in your application to do its thing
        return false;
      } else {
        //Pass it to the system, doesn't match your domain
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
        //Tell the WebView you took care of it.
        return true;
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
}

I know that seems a little counterintuitive as you would expect return false; to completely circumvent the WebView, but this is not the case once you are using a custom WebViewClient.

Hope that helps!

Trott
  • 66,479
  • 23
  • 173
  • 212
devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Thanks for the code but unfortunately this does not work either. I tried your code, external browser loads the url (www.example.org) as soon as I start the application. – ace Feb 05 '11 at 18:44
  • That sounds more like an issue with the if statement that chooses the path. I have updated the example now that you have posted code using the same if-else check that you did (we're both assuming that statement evaluates your URLs correctly). – devunwired Feb 07 '11 at 14:30
  • Hi it still does not work. Again external browser handles the url no matter what the opposite of my code. It seems google docs on this – ace Feb 07 '11 at 17:54
  • http://developer.android.com/resources/tutorials/views/hello-webview.html is either wrong or misleading. Esp note in that doc they say: "Returning true says that the method has handled the URL and the event should not propagate (in which case, an Intent would be created that's handled by the Browser application)." – ace Feb 07 '11 at 17:55
  • Can you post the rest of your Activity code? If the browser handles your clicks regardless...it sounds like your WebViewClient implementation is not properly attached to the WebView... – devunwired Feb 07 '11 at 21:08
  • 2
    Thanks Wireless Designs, that worked. Note that "request" in your code should be Uri.parse(url) instead. – rado Feb 24 '11 at 18:58
  • My answer for newbies and those who got confused by android documentation. Please check. http://stackoverflow.com/a/26059756/609782 – Darpan Sep 26 '14 at 12:44
5

If you can't be bothered to explain what "does not work properly" means, we can't be bothered to give you much specific help.

Use shouldOverrideUrlLoading(). Examine the supplied URL. If it is one you want to keep in the WebView, call loadUrl() on the WebView with the URL and return true. Otherwise, return false and let Android handle it normally.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

Add the following to your activity

@Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(Uri.parse(url).getHost().endsWith("192.168.1.34")) {
                    view.loadUrl(url);
                    Log.d("URL => ", url);    // load URL in webview
                    return false;
                }

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                view.getContext().startActivity(intent); // Pass it to the system, doesn't match your domain 
                return true;
            }
Suraj Lulla
  • 489
  • 5
  • 12