Search code examples
xamarinwebxamarin.formsxamarin.androidandroid-webview

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));

            Control.LoadUrl(Control.Url);

 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 + "");
                

            }

        }

Solution

  • 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)
            {
                base.OnElementChanged(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;
                    SetNativeControl(webView);
    
    
                }
                if (e.OldElement != null)
                {
                    Control.RemoveJavascriptInterface("jsBridge");
                    var hybridWebView = e.OldElement as HybridWebView;
                    hybridWebView.Cleanup();
                }
                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");
                    Control.LoadUrl(“www.google.com”);
    
    
                }
            }
       
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    ((HybridWebView)Element).Cleanup();
                }
                base.Dispose(disposing);
            }
        }
    }