[FIXED] Unable to get event listener call back from WebView in Xamarin forms


I have my mobile application build using Xamarin forms, which consist of a webView loaded into my application. The WebView Consist of buttons and icons being loaded. I have a requirement to make the button event Listener being called , so that I can do the corresponding functionality. Can anyone help me in how to get the button events being called in Xamarin forms.

source code:

Control.SetWebViewClient(new HybridWebViewClient());
            Control.SetWebChromeClient(new MyWebClient(mContext));


 internal class HybridWebViewClient : WebViewClient

            public override async void OnPageFinished(Android.Webkit.WebView view, string url)

                base.OnPageFinished(view, url);

                int i = 10;
                while (view.ContentHeight == 0 && i-- > 0)
                    await Task.Delay(1000);
                    // find the particular button
                    string funcurl = " var btn1 =document.getElementsByClassName('inspectionStartingBtnClose')[0]; if(btn1 != null){btn1.addEventListener('click', function() { alert('dosomething'); })";
                    view.LoadUrl("javascript: r(function(){" + funcurl + "");




using System;
using Android.Content;
using TestProject.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Vinspector.HybridWebView), typeof(HybridWebViewRenderer))]

namespace TestProject.Droid
    public class HybridWebViewRenderer : WebViewRenderer
    const string JavascriptFunction= "document.getElementById('btnId').onclick= function(data){jsBridge.submissionAction(data);}";`

        Context _context;

        public HybridWebViewRenderer(Context context) : base(context)
            _context = context;

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
            if (Control == null)
                Console.WriteLine("print inside  control is null!!!");
                var webView = new Android.Webkit.WebView(_context);
                webView.Settings.JavaScriptEnabled = true;
                webView.Settings.DatabaseEnabled = true;
                webView.Settings.DomStorageEnabled = true;

            if (e.OldElement != null)
                var hybridWebView = e.OldElement as HybridWebView;
            if (e.NewElement != null)
                Control.Settings.JavaScriptEnabled = true;
                Control.Settings.DatabaseEnabled = true;
                Control.Settings.DomStorageEnabled = true;
               webView.SetWebViewClient(new `JavascriptWebViewClient(this,string.Format("javascript: {0}", JavascriptFunction)));`
                Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");


        protected override void Dispose(bool disposing)
            if (disposing)

Answered By – I'm Coder

Answer Checked By – Robin (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published