1

So i want to be able to monitor the URL my WebView application is loading. The main thing im trying to accomplish is to keep the WebView inside a specific Website or at least by able to detect when the user is leaving the website's URL.

Example. If i have the base URL of www.google.com and when the user clicks Images, he gets "http://www.google.com/imghp" However it still has the base URL of www.google.com, so if there is a way to monitor when the base URL is no longer www.google.com, such as when a user clicks a search result and is then brought to a different website completely.

The purpose of doing this is that i want to display to the user a dialog or Toast to let them know they are leaving the website that this WebView app is created for.

Any Code, Idea's or suggestions would be greatly appreciated.

Example Code

 @Override
 public void onLoadResource(WebView view, String url)
    {
        if (url.equals("http://www.google.com"))
        {
            //do your own thing here
            view.loadUrl(url);
        }
        else
        {
            AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(Webview_Main.this);
            localBuilder2.setTitle("Hey Wait!");
            localBuilder2.setMessage("You currently are about to leave the website\n\nIf you want to stay inside this website, Please click the Go Back button below");
            localBuilder2.setIcon(R.drawable.ic_launcher);
            localBuilder2.setPositiveButton("Go Back",
                    new DialogInterface.OnClickListener() {
                public void onClick(
                        DialogInterface paramDialogInterface,
                        int paramInt) {
                    web.goBack();
                }
            });
            localBuilder2.setNegativeButton("Continue",
                    new DialogInterface.OnClickListener() {
                public void onClick(
                        DialogInterface paramDialogInterface,
                        int paramInt) {

                }
            });

            localBuilder2.show();
        };
            super.onLoadResource(view, url);
        }    

However this above code doesnt work, only keeps poping up the alert dialog since the URL maybe different.

Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
  • 1
    Do a search for [`WebViewClient#shouldOverrideUrlLoading(...)`](http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29) and [`WebViewClient#shouldInterceptRequest(...)`](http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest%28android.webkit.WebView,%20java.lang.String%29). Plenty of examples out there on how to intercept urls that are about to be loaded. – MH. Apr 10 '13 at 23:03
  • possible duplicate of [WebView — clicking URLs opens default browser](http://stackoverflow.com/questions/2378800/webview-clicking-urls-opens-default-browser) – Marcin Orlowski Apr 10 '13 at 23:04
  • @MH. Thanks for the comment, I've add some code above can you take a look. – Jaison Brooks Apr 11 '13 at 16:07

1 Answers1

1

I have resolved my issue with the following code.

 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {
           if (url.startsWith("http://www.google.com"))
            { view.loadUrl(url);
            progressBar.setVisibility(View.VISIBLE);
            }
          else
            { Toast.makeText(getBaseContext(),
            "Your about to leave Google.com",
            Toast.LENGTH_LONG).show();
    }
 }

Thanks @MH. for the suggestion

Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79