How To Load A Web Page Using WebView In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS).

In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Prerequisites

Visual Studio 2015 Update 3.

The steps, given below are required to be followed in order to load a Webpage, using WebView in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio
or click (Ctrl+Shift+N).

Visual Studio 2015

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Visual Studio 2015

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.

Now, select Resource-->Layout-->double click to open main.axml page. You need to select the source to write XAML code.

Choose the Designer Window, if you want design it, and you can design your app.

Visual Studio 2015

Step 4

After opening main.axml, file will open the main page designer. You can design the page, as per you desire.

Visual Studio 2015

Now, delete the Default hello world button, go to the source panel and you can see the button coding. You need to delete it.

After deleting XAML code, now delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.

You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls.

You need to drag and drop the WebView.

Visual Studio 2015

Step 6

Now, go to the properties Window. You need to edit the WebView's Id value.

(EX:android:id="@+id/LocalWebView" ).

Visual Studio 2015

Step 7

In this step, go to the Main.axml page source panel. Note, the WebView's Id value.

Main.axml

  1. <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/LocalWebView" />  
Visual Studio 2015

Step 8

In this step, go to MainActivity.cs page. Add Webkit namespace.

MainActivity.cs

using Android.Webkit;

Visual Studio 2015

Step 9

In this step, go to MainActivity.cs page, write the code, mentioned below between OnCreate() method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource   
  4.     SetContentView(Resource.Layout.Main);  
  5.     WebView webView = FindViewById < WebView > (Resource.Id.LocalWebView);  
  6.     webView.SetWebViewClient(new WebViewClient());  
  7.     webView.LoadUrl("http://www.xamarin.com");  
  8.     webView.Settings.JavaScriptEnabled = true;  
  9.     webView.Settings.BuiltInZoomControls = true;  
  10.     webView.Settings.SetSupportZoom(true);  
  11.     webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;  
  12.     webView.ScrollbarFadingEnabled = false;  
  13. }  
Visual Studio 2015

Step 10

If you have Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click Run option.

Visual Studio 2015

Output

After a few seconds, the app will start running on your phone.

You will see the the Web Page View is successful.

Visual Studio 2015

Zoom control is working successfully.

Visual Studio 2015

Summary

This was the process of how to load a Webpage, using WebView in Xamarin Android app, using Visual Studio 2015.

 


Similar Articles