How to detect hyperlink click in a webview android with C#

This post is about detecting hyperlink click in a Android webview with C#. This can be useful in two scenarios.

  1. You need to open an activity when user clicks on some hyperlink
  2. You need to do something when user clicks on some hyper link.

You can achieve this by overriding the ShouldOverrideUrlLoading() method of WebViewClient class.

Here is the implementation. In this code, my web application contains a special hyperlink with “home” protocol, if user clicks on the link, I need to show the Main activity, else user need to navigate to the required url.

//Setting the custom web view client to the WebView.
webView.SetWebViewClient (new CustomWebViewClient ());

//Implementation of CustomWebViewClient
private class CustomWebViewClient : WebViewClient
{
	public override bool ShouldOverrideUrlLoading (WebView view, string url)
	{
		if (url.StartsWith ("home:")) {
			view.Context.StartActivity (typeof(MainActivity));
			return true;
		}
		view.LoadUrl (url);
		return true;
	}
}

Happy Programming :)