Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I redirect the phone dialer from WebView when I click on a phone number?

I have implemented a custom web view renderer in Droid project.
How can I redirect the Phone Dialer from WebView when I click on a phone number in WebView?

Any help is appreciated. Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try the following code:

 public class MyWebViewRenderer : WebViewRenderer
{
    public MyWebViewRenderer(Context context) : base(context) { }
    static MyWebView _xwebView = null;
    WebView _webView;
   
    class ExtendedWebViewClient : Android.Webkit.WebViewClient
    {
         public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            var tel = "tel:";
            if (url != null)
            {
                if (url.StartsWith(tel))
                {

                    var uri = Android.Net.Uri.Parse(url);
                    var intent = new Intent(Intent.ActionView, uri);
                    intent.SetFlags(ActivityFlags.NewTask);
                    Android.App.Application.Context.StartActivity(intent);
                    return true;
                }
                
            }
            return false;
        }
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);
        _xwebView = e.NewElement as MyWebView;
        _webView = Control;

        if (e.OldElement == null)
        {
            _webView.SetWebViewClient(new ExtendedWebViewClient());
          
        }
    }

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading