extends WebViewClient and check the url : WebView « UI « Android






extends WebViewClient and check the url

   
package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Test extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);
    // Add a client to the view
    webview.setWebViewClient(mClient);
    webview.loadUrl("http://www.google.com");
    setContentView(webview);
  }

  private WebViewClient mClient = new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      Uri request = Uri.parse(url);

      if (TextUtils.equals(request.getAuthority(), "www.google.com")) {
        // Allow the load
        return false;
      }

      Toast.makeText(Test.this, "Sorry, buddy", Toast.LENGTH_SHORT)
          .show();
      return true;
    }
  };
}

   
    
    
  








Related examples in the same category

1.Adding WebView to Activity, load URL and grab focus
2.Load local resource into WebView
3.Enable Javascript for WebView
4.Load remote web page for WebView
5.Using WebView to load url
6.Using WebView to load static html string
7.Load static html file into WebView
8.Load Url to WebView
9.Load image from remote host to WebView
10.Add a client to the web view
11.Sample creating 10 webviews.