Alright, let’s make a Transparent Xamarin Forms Webview!

Have you ever wanted to add a WebView to your Xamarin Forms app and make it’s background transparent ? Let’s say you need to display some custom Text Label’s or some custom control with loads of customization to it’s content or sub controls ?
Whereas your first solution would be to go for a WebView control, but you need to make that WebView Transparent to match the background color of the page ? If you had gone through or going through such requirements, then this article is for you.

WebView for Fancy Stuff…

We all know WebView is usually used for displaying Web Content right ? but then also we could use it to display complicated customized text or content, but then there is one problem, matching the Page background to the WebView control, because it’s impossible to set the background transparent of the WebView in Xamarin Forms.

Solution ?

Yeah you guess right! Custom Renderers for the RESCUE! WOOT!

So here is how I implemented my Transparent WebView with life-saving Custom Renderers in Xamarin Forms.

PCL implementation

So as usual we need to sub-class the default Xamarin Forms WebView control with our Transparent WebView control as follows.

namespace TransparentWebViewXamForms
{
    /// <summary>
    /// Subclassing our Transparent WebView from 
    /// the default Xamarin Forms WebView control
    /// </summary>
    public class TransparentWebView : WebView
    {

    }
}

 

Android implementation

So as of our Android implementation we need to access the native control counterpart for the Xamarin Forms WebView using the WebViewRenderer.

[assembly: ExportRenderer(typeof(TransparentWebView), typeof(TransparentWebViewRenderer))]
namespace TransparentWebViewXF.Droid
{
    public class TransparentWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            // Setting the background as transparent
            this.Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
        }
    }
}

 

We access the Control property which holds the native Android WebView control which deals with the Xamarin Forms WebView. As you can see above we set the Control Background color to Transparent through SetBackgroundColor() method. BOOM! You are good to go with Android!

iOS implementation

Now let’s dive into iOS, well its the same as Android, the trick is to set the Control’s background color to Transparent.

[assembly: ExportRenderer(typeof(TransparentWebView), typeof(TransparentWebViewRenderer))]
namespace TransparentWebViewXF.iOS
{
    public class TransparentWebViewRenderer : WebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
			
	    // Setting the background as transparent
            this.Opaque = false;
            this.BackgroundColor = Color.Transparent.ToUIColor();
        }
    }
}

 

But also we need to set the Opaque property to false as show above, otherwise the control wouldn’t render as transparent. Then as usual we set the BackgroundColor property.

So let’s try it out!

Too lazy to try it out ? 😛 No worry, I would probably be bored to try it out unless I actually need to. haha.

So here goes the sample implementation, if you have implemented the above properly, then use the below test code and run your app!

You can see below, I’m setting the Page’s background color to Red and the StackLayout’s background color to Yellow. Now the WebView should be rendered as Transparent and the Yellow background should be visible. 🙂

MainPage = new ContentPage
{
	BackgroundColor = Color.Red,

	Content = new StackLayout
	{
		BackgroundColor = Color.Yellow,
		VerticalOptions = LayoutOptions.CenterAndExpand,
		Children = {
			new Label {
				XAlign = TextAlignment.Center,
				TextColor = Color.Red,
				FontSize = 20,
				Text = "Welcome to Xamarin Forms!"
			},

			new TransparentWebView() { 
				Source = new HtmlWebViewSource()
				{
					Html = @"<html><body>
						<center>
						<h1>Awesome Transparent WebView!</h1>
						<p>by ÇøŋfuzëÐ SøurcëÇødë</p>
						<br> call us : <a href='tel:+6576216231'>1-847-555-5555</a>
						<br> email us : <a href='mailto:webmaster@example.com'>blah@blah.com</a>
						<br> visit us : <a href='http://www.awesome.com'>Awesome.com</a>
						</center>
						</body></html>",
				},
				HeightRequest = 400
			},

			new Label {
				XAlign = TextAlignment.Center,
				TextColor = Color.Red,
				FontSize = 20,
				Text = "And that's the end of it! Have fun!"
			},
		}
	}
};

 

BOOM! THERE YOU GO! 😀

Simulator Screen Shot 2 Mar 2016, 10.23.43 PM Nexus 4 (Lollipop) Screenshot 1

Alright! Now go crazy with it fellas!

Stay Awesome! 😉

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s