Android webview capture link clicks-ThrowExceptions

Exception or error:

The application i’m developing has a webview, i need to capture webview request click events(eg: click on a link, click on a button, click on a youtube video play button such as…)

How to solve:

This can be done

you have to set a WebViewClient to your WebView. this is how to do that.

WebView webView;//make sure to initialize  
webView.setWebViewClient(webViewClient);

WebViewClient webViewClient= new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView  view, String  url){
        return true;
    }
    @Override
    public void onLoadResource(WebView  view, String  url){
        if( url.equals("http://yoururl.com") ){
            // do something
        }
    }
}

###

use this code it’s work for me

webview.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                return true;
            }
        });

###

You can do something like this:

WebView myWebView  = (WebView) findViewById(R.id.yourWebView);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("yourLink");

but then don’t forget to create a WebViewClient:

public class MyWebViewClient extends WebViewClient {

     @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            return true;
        }
}

Leave a Reply

Your email address will not be published. Required fields are marked *