Android Webview - Intercept Clicks

Android WebView - Intercept clicks

Then you have to set a WebViewClient to your WebView and override shouldOverrideUrlLoading and onLoadResource methods. Let me give you a simple example:

WebView yourWebView; // initialize it as always...
// this is the funny part:
yourWebView.setWebViewClient(yourWebClient);

// somewhere on your code...
WebViewClient yourWebClient = new WebViewClient(){
// you tell the webclient you want to catch when a url is about to load
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
// here you execute an action when the URL you want is about to load
@Override
public void onLoadResource(WebView view, String url){
if( url.equals("http://cnn.com") ){
// do whatever you want
}
}
}

How to intercept link requests in a webView?

This can be achieved by setting the webviewclient eg.

mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://blablablacatchurl")) {

//we have intercepted the desired url call
return true;
}

return super.shouldOverrideUrlLoading(view, url);
}
});

If you want to see if there is a way to deep link to the facebook app, just package the url in an intent and fire it off and see what responds. If facebook doesn't catch it I don't think you can perform the second part of your request in any other way.

Android WebView URL intercept , i want to load only one website and it's pages , and nothing else

In both cases you have consumed the event. try something like below .

 webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().equals(host)) {
// Intercept URL Load url here return true if url is consumed
return true;
}
return super.shouldOverrideUrlLoading(view, request);
}
});

Or if you want to block all other links then you can use it like below .

 webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().equals(yourwebsite)) {

return super.shouldOverrideUrlLoading(view, request);
}
return true;
}
});

Keep that in mind that all other links will not work so this can be a bad impression on your app . So i suggest that you should open other links with a browser intent . Like below.

 webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().equals(yourwebsite)) {

return super.shouldOverrideUrlLoading(view, request);
}else{
try {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl()));
startActivity(browserIntent);
}catch (Exception e){
e.printStackTrace();
}
}
return true;
}
});

NOTE:- This is implementation for shouldOverrideUrlLoading(WebView view, WebResourceRequest request) which is applicable above API 21 . So you should also override shouldOverrideUrlLoading(WebView view, String url) for previous version in same way .



Related Topics



Leave a reply



Submit