Create Web View Android Application Using Xamarin Android In Visual Studio 2017

What are Xamarin and Xamarin Forms?

  • Xamarin is a cross-platform to develop Multi-Platform Applications.
  • Xamarin is a shared code(C#) but separately designs UIs Android(Java), Windows(C#) and an iOS.
  • Xamarin forums is UIs & Shared code (C#) are same. To develop multi-platforms Applications, run all the projects (Android, Windows and an IOS) at the same time.

Prerequisites

  • Visual Studio 2017 Enterprise.

The steps given below are required to be followed in order to create Web View Android in Xamarin Android, using Microsoft Visual Studio 2017.

Step 1

  • Go to Visual Studio 2017.
  • Click File -> New -> Project.

    Xamarin

Step 2 

In this step, proceed as shown below.
  • Click C# -> Android -> Web View app (Android).
  • Enter the Application Name.
  • Web View = Create an Android Web View Application, whicht uses the Razor templating engine.

    Xamarin

Step 3

Afterwards, go to

  • Open Solution Explorer Window.
  • Now, click -> cs.

    Xamarin

Step 4

In this step, go to cs page, insert the code given below in MainActivity.cs aand save it.

Xamarin

Xamarin
  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Webkit;  
  7. using Android.Widget;  
  8. using Android.OS;  
  9. using WebView_Android.Views;  
  10. using WebView_Android.Models;  
  11. using Android.Graphics;  
  12.   
  13. namespace WebView_Android  
  14. {  
  15.     [Activity(Label = "WebView_Android", MainLauncher = true)]  
  16.     public class MainActivity : Activity  
  17.     {  
  18.         protected override void OnCreate(Bundle bundle)  
  19.         {  
  20.             base.OnCreate(bundle);  
  21.   
  22.             SetContentView(Resource.Layout.Main);  
  23.             var webView = FindViewById<WebView>(Resource.Id.webView);  
  24.             webView.Settings.JavaScriptEnabled = true;  
  25.             webView.SetWebViewClient(new HybridWebViewClient());  
  26.             // Render the view from the type generated from RazorView.cshtml  
  27.             webView.LoadUrl("http://www.google.co.in");  
  28.            
  29.         }  
  30.         public class HybridWebViewClient : WebViewClient  
  31.         {  
  32.             public override bool ShouldOverrideUrlLoading(WebView view, string url)  
  33.             {  
  34.   
  35.                 view.LoadUrl(url);  
  36.                 return true;  
  37.             }  
  38.             public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)  
  39.             {  
  40.                 base.OnPageStarted(view, url, favicon);  
  41.             }  
  42.             public override void OnPageFinished(WebView view, string url)  
  43.             {  
  44.                 base.OnPageFinished(view, url);  
  45.             }  
  46.             public override void OnReceivedError(WebView view, [GeneratedEnum] ClientError errorCode, string description, string failingUrl)  
  47.             {  
  48.                 base.OnReceivedError(view, errorCode, description, failingUrl);  
  49.             }  
  50.   
  51.   
  52.   
  53.   
  54.         }  
  55.     }  
  56.   
  57. }  

Step 5

  • Click Build menu and go to Configuration Manager.
  • Configure your Android Application to depoly & build Setting. Click Close.

    Xamarin

Step 6

In this step, select Build & depoly option, followed by clicking to start your Application.

Now, go to Run option, choose Debug and the list of an Android Simulator. You can choose any API Level Simulator and run it.

Xamarin

Step 7

Output

  • After a few seconds, the app will start running on your Android Simulator. You will see your app is working & is created successfully.

    Xamarin
  • Your Web View Android Application is created succesfully.

Conclusion

Thus, we learned how to create Web View Application in an Android, using Xamarin on Visual Studio 2017.

Up Next
    Ebook Download
    View all
    Learn
    View all