Xamarin.Forms - Enable Default Zooming In Webview

Introduction

 
Xamarin.Forms - Enable Default Zooming In Webview
 
Xamarin.Forms code runs on multiple platforms, each of which has its own filesystem. This means that reading and writing files are most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You will learn more by going through the steps yourself.
 
Create a new or existing Xamarin forms (.Net standard) Project with Android and iOS Platform. Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
 
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
 
Now, select the blank app and target platforms - Android, iOS, and Windows (UWP).
 
Subsequently, go to the solution. There, you will get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 

Create a Custom Webview

 
Here I'm going to create a custom webview inherited from webview for custom rendering to enable zooming.
 
CustomWebView.cs
  1. public class CustomWebView : WebView  
  2. {  
  3. }  
Android Implementation
 
There are two ways in Android.
 
Use Platform Specific code
 
The following code snippets enable the default zooming in webview.
 
MyWebView.xaml.cs
  1. if (Device.RuntimePlatform == Device.Android)  
  2.             {  
  3.                 webView.On<Android>().EnableZoomControls(true);  
  4.                 webView.On<Android>().DisplayZoomControls(true);  
  5.             }  
Custom Renderer
 
Here, create a custom renderer for enabling zooming in Android.
 
CustomWebViewRenderer.cs
  1. [assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]  
  2. namespace PDFPOC.Droid  
  3. {  
  4.     public class CustomWebViewRenderer : WebViewRenderer  
  5.     {  
  6.         public CustomWebViewRenderer(Context context) : base(context)  
  7.         {  
  8.         }  
  9.         protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)  
  10.         {  
  11.             base.OnElementChanged(e);  
  12.   
  13.             if (e.NewElement != null)  
  14.             {  
  15.                 var pdfView = Element as CustomWebView;  
  16.                 Control.Settings.AllowUniversalAccessFromFileURLs = true;  
  17.                 Control.Settings.SetSupportZoom(true);  
  18.                 Control.Settings.BuiltInZoomControls = true;  
  19.                 Control.Settings.DisplayZoomControls = true;  
  20.             }  
  21.         }  
  22.     }  
  23. }  
iOS Implementation
 
Here, create a custom renderer for enabling the webview default zooming in iOS.
 
CustomWebViewRenderer.cs
  1. [assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]  
  2.   
  3. namespace PDFPOC.iOS  
  4. {  
  5.     public class CustomWebViewRenderer : WebViewRenderer  
  6.     {  
  7.         protected override void OnElementChanged(VisualElementChangedEventArgs e)  
  8.         {  
  9.             base.OnElementChanged(e);  
  10.   
  11.             if (NativeView != null && e.NewElement != null)  
  12.             {  
  13.                 var webView = NativeView as UIWebView;  
  14.   
  15.                 if (webView == null)  
  16.                     return;  
  17.   
  18.                 webView.ScalesPageToFit = true;  
  19.             }  
  20.         }  
  21.     }  
  22. }  

Consuming the CustomWebview

 
Here, consume the Custom Webview in your xaml.
 
Add Namespace
  1. xmlns:controls="clr-namespace:PDFPOC.Controls"  
Add your Custom Webview
 
MyWebPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"    
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.              xmlns:controls="clr-namespace:PDFPOC.Controls"    
  7.              mc:Ignorable="d"    
  8.              x:Class="PDFPOC.MyWebPage">    
  9.     <ContentPage.Content>    
  10.         <StackLayout>    
  11.             <controls:CustomWebView x:Name="webView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>    
  12.         </StackLayout>    
  13.     </ContentPage.Content>    
  14. </ContentPage>     
MyWebPage.xaml.cs
  1. public partial class MyWebPage : ContentPage  
  2.     {  
  3.         public MyWebPage()  
  4.         {  
  5.             InitializeComponent();  
  6.             webView.Source = "https://xamarinmonkeys.blogspot.com/";  
  7.               
  8.         }  
  9.              
  10.     }  
Click the "Play" button to try it out.

Android
 
You can test zoom by emulator also use Shift + ctrl + Left click and mouse move
 
Xamarin.Forms - Enable Default Zooming In Webview
 
iOS
 
Simulator also supports Zoom. Use Alt + Left click.
 
Xamarin.Forms - Enable Default Zooming In Webview
 
Wow, it's working.
 
I hope you have understood how to enable webview default zooming in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)


Similar Articles