Download Code

Xamarin WebView Example Android

In this tutorial you will learn how to xamarin webView example android application.Simply you can say how to display web page in a webview.

WebView

An extension of Android’s View class.Allows to display web pages.

This View uses WebKit rendering engine to display web pages.

Including methods to navigate forward or backward,Zoom in,out,History etc.

Most importantly to load web pages in web view use INTERNET permission in you android manifest file.

For More(WebView),detail.

Basically we will load a Android Codec Web page within android app activity using web view.

So, let’s start go ahead and create Xamarin WebView Example android application project.

After creating android application project.Go to Main.axml now you will add web view.

Basically there are two ways whether full screen or inside an application as one component.

No need to add any thing else just use this xml code.

Full Screen XML

Xamarin WebView,Xamarin Webview example,xamarin webview example android,webview example android,webview

As Component XML

Xamarin WebView,Xamarin Webview example,xamarin webview example android,webview example android,webview

You will use full screen web view.

Go to MainActivity.cs class/activity,by default it will look like this.

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Webkit;

namespace XamarinWebView
{
	[Activity(Label = "XamarinWebView", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);
			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
		}
	}
}

Now declare a variable of Web View class.

Next refer the id of web view from xml in activity and store it in web View variable.

Call loadUrl() method of WebView class to load specific webpages in web view.

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Webkit;

namespace XamarinWebView
{
	[Activity(Label = "XamarinWebView", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		WebView myWeb;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);
			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			myWeb = FindViewById(Resource.Id.webview);
			myWeb.LoadUrl("http://www.androidcodec.com");
		}
	}
}

Now,don’t ever forget to add INTERNET permission in your android manifest file.

Go to Propoerties > Android Manifest.xml and add this permission.

Android Manifest.xml

Xamarin WebView,Xamarin Webview example,xamarin webview example android,webview example android,webview

Now run your Xamarin WebView Example android application project.

Xamarin WebView,Xamarin Webview example,xamarin webview example android,webview example android,webview

If web page contains javascript code it will not run until you enable it.

Use WebSetting of webview and enable javascript using JavaScriptEnabled.

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Webkit;

namespace XamarinWebView
{
	[Activity(Label = "XamarinWebView", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		WebView myWeb;
		WebSettings myWebSetting;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);
			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			myWeb = FindViewById(Resource.Id.webview);
			myWebSetting = myWeb.Settings;
			myWebSetting.JavaScriptEnabled = true;
			myWeb.LoadUrl("http://www.androidcodec.com");
		}
	}
}

When you click any post it will open in browser.

For this issue use webViewClient class to load url using setWebViewClient().

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Webkit;

namespace XamarinWebView
{
	[Activity(Label = "XamarinWebView", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{

		WebView myWeb;
		WebSettings myWebSetting;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			myWeb = FindViewById(Resource.Id.webview);
			myWebSetting = myWeb.Settings;
			myWebSetting.JavaScriptEnabled = true;
			myWeb.SetWebViewClient(new nextWebClient());
			myWeb.LoadUrl("http://www.androidcodec.com");
		}
	}
}

Create nextWebClient class implement shouldOverrideUrlLoading().

Two parameters WebView and url.

WebView to display clicked webpages and url webpage url using loadUrl() method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
namespace XamarinWebView
{
	[Activity(Label = "nextWebClient")]
	public class nextWebClient : WebViewClient
	{
		public bool shouldOverrideUrlLoading(WebView view, String url)
		{
			view.LoadUrl(url);
			return true;
		}
	}
}

Now run your Xamarin WebView Example Android application project.

Xamarin WebView,Xamarin Webview example,xamarin webview example android,webview example android,webview

That’s it for Xamarin WebView Example Android tutorial.

Thanks.