locked
Is it possible to add android zoom buttons on only one webview? RRS feed

  • Question

  • User395616 posted

    I have one web view where I need the zoom buttons enabled, but they look tacky and get in the way of other web views.

    I know how to set the zoom buttons up using this.Control.Settings.SetSupportZoom(false); , but it applies to every single web view.

    In my CustomWebViewRenderer I have no way to reference what url I'm actually using to open the webview. Instead, the url just returns "file:///android_asset/"

    Any ideas of how I co do something like... if(url == "example.com") this.Control.Settings.SetSupportZoom(true) else this.Control.Settings.SetSupportZoom(false)

    It doesn't have to be exactly that, just the same effects.

    Monday, July 6, 2020 2:10 PM

Answers

  • User369978 posted

    Here we need webview custom renderer , we could detect the url in method OnPageFinished inside class WebViewClient ,the method triggers when webview finish loading .

    Simple sample

        [assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyRenderer))]
        namespace App1.Droid
        {
            class MyRenderer : WebViewRenderer
            {
                readonly Context c = null;
                public MyRenderer(Context context):base(context)
                {
                    c = context;
                }
    
                protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
                {
                    base.OnElementChanged(e);
                    if (e.OldElement == null)
                    {
                        // lets get a reference to the native control
                        var webView = (global::Android.Webkit.WebView)Control;
                        webView.SetWebViewClient(new MyWebViewClient());
                        webView.SetInitialScale(0);
                        webView.Settings.JavaScriptEnabled = true;
                    }
                }
            }
    
            public class MyWebViewClient : WebViewClient
            {
                public override void OnPageFinished(Android.Webkit.WebView view, string url)
                {
                    base.OnPageFinished(view, url);
    
                    if (url == "example.com")
                    {
                        view.Settings.SetSupportZoom(true);
                    }      
                    else
                    {
                        view.Settings.SetSupportZoom(false);
                    }              
                }
            }
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 7, 2020 3:25 AM

All replies

  • User369978 posted

    Here we need webview custom renderer , we could detect the url in method OnPageFinished inside class WebViewClient ,the method triggers when webview finish loading .

    Simple sample

        [assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyRenderer))]
        namespace App1.Droid
        {
            class MyRenderer : WebViewRenderer
            {
                readonly Context c = null;
                public MyRenderer(Context context):base(context)
                {
                    c = context;
                }
    
                protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
                {
                    base.OnElementChanged(e);
                    if (e.OldElement == null)
                    {
                        // lets get a reference to the native control
                        var webView = (global::Android.Webkit.WebView)Control;
                        webView.SetWebViewClient(new MyWebViewClient());
                        webView.SetInitialScale(0);
                        webView.Settings.JavaScriptEnabled = true;
                    }
                }
            }
    
            public class MyWebViewClient : WebViewClient
            {
                public override void OnPageFinished(Android.Webkit.WebView view, string url)
                {
                    base.OnPageFinished(view, url);
    
                    if (url == "example.com")
                    {
                        view.Settings.SetSupportZoom(true);
                    }      
                    else
                    {
                        view.Settings.SetSupportZoom(false);
                    }              
                }
            }
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 7, 2020 3:25 AM
  • User395616 posted

    Thank you so much! I was editing in the wrong section of OnPageStarted

    This worked exactly as expected.

    Wednesday, July 8, 2020 3:59 PM