4

I want to block a link from loading within a Webview.

Code

public class WebMy extends Activity {


   
    private WebView mWebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
            setContentView(R.layout.pantalla);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);  
            
    
             
            mWebview  = new WebView(this);
            mWebview.setWebViewClient(new WebViewClient()); 
            mWebview.getSettings().setJavaScriptEnabled(true); // Enable JavaScript.

            mWebview .loadUrl("http://www.myweb.com");
            setContentView(mWebview );
           
    }

Potential Solution

public class MyWebViewClient extends WebViewClient {
    public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) {
         // Do something with the event here.
         return true;
    }

    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.google.com")) {
             // This is my web site, so do not override; let my WebView load the page.
             return false;
        }

        // Reject everything else.
        return true;
    }
}

I don´t know how I have to use this in my code. For example, if I want to block this url http://www.myweb.com/pepito. How can I do this with this code? Thank you.

AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
user3733523
  • 151
  • 1
  • 2
  • 11

5 Answers5

3

shouldOverrideUrlLoading will examine the web page URL loaded into the WebView and all URLs loaded within the page content.

public class MyWebViewClient extends WebViewClient {
    public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
         
         return true;
    }

    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        if (Uri.parse(url).getHost().equals("http://www.myweb.com/pepito")) {
             // This is my web site, so do not override; let the WebView load the page.
             return false;
        }

        // Reject everything else.
        return true;
    }
}
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
  • Replace your code line mWebview.setWebViewClient(new WebViewClient()); with mWebview.setWebViewClient(new MyWebViewClient()); – Febi M Felix Mar 10 '16 at 09:45
  • Provide another check inside the if statement. Like , if(Uri.parse(url).getHost().equals("http://www.myweb.com/pepito") || Uri.parse(url).getHost().equals("2nd url to block")) {} – Febi M Felix Mar 10 '16 at 09:50
  • Thank you!!! my last question, is that possible to block a link like this href="mailto:pepito@gmail.com", I have try but not works – user3733523 Mar 10 '16 at 09:55
2

this will cause nothing to happen when the link "http://www.myweb.com/pepito" is clicked

public class MyWebViewClient extends WebViewClient {
    public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) {
         // Do something with the event here
         return true;
    }

    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        return url.equals("http://www.myweb.com/pepito");
    }
}
Björn Kechel
  • 7,933
  • 3
  • 54
  • 57
0
    webView.setWebViewClient(new myWebClient()); 
// add this while initializing webview


// then do add following code





public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
           // super.onPageStarted(view, url, favicon);
            if (Uri.parse(url).getHost().contains("https://qa.mstitute.com/test/build/#!/")) {
                // This is my web site, so do not override; let my WebView load the page
                Log.d("web","block");

            }else{
                super.onPageStarted(view, url, favicon);
            }

            Log.d("web","Started");
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            if (Uri.parse(url).getHost().equals("https://qa.mstitute.com/test/build/#!/")) {
                // This is my web site, so do not override; let my WebView load the page
                Log.d("web","block");
                return false;
            }else {
                view.loadUrl(url);
                return true;
            }

        }
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request,
                                    WebResourceError error) {
            super.onReceivedError(view, request, error);
            Log.d("web","got an error");

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub





        }
    }
0

For Xamarin Developers (it can be adapted to Native also), this is an updated answer because the method ShouldOverrideUrlLoading(WebView view, string url) is obsolete:

[System.Obsolete]
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
    return !Uri.Parse(url).Host.Contains("YOUR_URL");
}

public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
    return !request.Url.ToString().Contains("YOUR_URL");
}
Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
-1

Solution 1

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val webView : WebView = findViewById(R.id.webView)
    webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            // This is my web site, so do not override; let the WebView load the page.
            if (Uri.parse(url).host == "google.com") return false
            // Reject everything else.
            return true
        }
     }
   }
 }

Solution 2

class MainActivity : AppCompatActivity() {
 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val webView : WebView = findViewById(R.id.webView)
    webView.webViewClient = MyWebViewClient()
}

private class MyWebViewClient : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        // This is my web site, so do not override; let the WebView load the page.
        if (Uri.parse(url).host == "google.com") return false
        // Reject everything else.
        return true
    }
  }
}

if (Uri.parse(url).host == "google.com") return false This code wont allow to go google

Cemil AKAN
  • 68
  • 1
  • 3