In this article, we will learn how to use WebView in Xamarin. If you are new to this series on Xamarin, then I suggest you read my previous articles of this series.
What is WebView?

Webview is a view, which is used to display HTML content or the Web content in your app.
Let’s begin.

Example of Load Static HTML in Webview

Create new Xamarin Android project. Click on the Blank android app, give it a meaningful name and then click OK.

Add a Webview in LinearLayout(Vertical)

Main.axml Code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:minWidth="25px"
  7. android:minHeight="25px">
  8. <android.webkit.WebView
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:id="@+id/webView" />
  12. </LinearLayout>
Preview



Open MainActivity.cs, get WebView control/view from Main Layout and Load HTML data in WebView, using the code given below.

MainActivity.cs
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4. using Android.Webkit;
  5. namespace WebViewHTMLPage
  6. {
  7. [Activity(Label = "WebViewHTMLPage", MainLauncher = true, Icon = "@drawable/icon",Theme ="@android:style/Theme.NoTitleBar")]
  8. public class MainActivity : Activity
  9. {
  10. //Creating Instance of Webview
  11. WebView webView;
  12. protected override void OnCreate(Bundle bundle)
  13. {
  14. base.OnCreate(bundle);
  15. // Set our view from the "main" layout resource
  16. SetContentView (Resource.Layout.Main);
  17. //Get webView WebView from Main Layout
  18. webView = FindViewById<WebView>(Resource.Id.webView);
  19. //HTML String
  20. string HTMLText = "<html>" + "<body>Sample <b>HTML</b> Text<br/><br/>"+
  21. "<span style='color:red;'>Application created by: Anoop Kumar Sharma<span></body>" +
  22. "</html>";
  23. //Load HTML Data in WebView
  24. webView.LoadData(HTMLText, "text/html", null);
  25. }
  26. }
  27. }
Build and run the Application.

Example of open Web Page or URL in WebView

Let’s add a layout (Horizontal) and WebView controls in LinearLayout (Vertical) in Main Layout. Add a plain text and button widget In Horizontal layout, as shown below.

When a user types URL or a Website name in Plaintext view and presses the Go button, we will open a Webpage in Webview widget.
Main.axml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <LinearLayout
  7. android:orientation="horizontal"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/linearLayout1">
  11. <EditText
  12. android:layout_height="match_parent"
  13. android:id="@+id/txtURL"
  14. android:layout_weight="1"
  15. android:layout_width="match_parent"
  16. android:hint="www.google.com" />
  17. <Button
  18. android:text="Go"
  19. android:layout_width="wrap_content"
  20. android:layout_height="match_parent"
  21. android:id="@+id/btnGo" />
  22. </LinearLayout>
  23. <android.webkit.WebView
  24. android:layout_width="match_parent"
  25. android:layout_height="match_parent"
  26. android:id="@+id/webView" />
  27. </LinearLayout>
MainActivity.cs Code
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4. using Android.Webkit;
  5. namespace WebViewinXamarin
  6. {
  7. [Activity(Label = "WebViewinXamarin", MainLauncher = true, Icon = "@drawable/icon",Theme ="@android:style/Theme.NoTitleBar")]
  8. public class MainActivity : Activity
  9. {
  10. //Creating Instance of Button,TextView, WebView
  11. Button btnGo;
  12. TextView txtURL;
  13. WebView webView;
  14. protected override void OnCreate(Bundle bundle)
  15. {
  16. base.OnCreate(bundle);
  17. // Set our view from the "main" layout resource
  18. SetContentView(Resource.Layout.Main);
  19. //Get txtURL TextView, btnGo Button,webView WebView from Main Resource Layout.
  20. txtURL = FindViewById<TextView>(Resource.Id.txtURL);
  21. btnGo = FindViewById<Button>(Resource.Id.btnGo);
  22. webView = FindViewById<WebView>(Resource.Id.webView);
  23. //SetWebViewClient with an instance of WebViewClientClass
  24. webView.SetWebViewClient(new WebViewClientClass());
  25. webView.LoadUrl(txtURL.Text);
  26. //Enabled Javascript in Websettings
  27. WebSettings websettings = webView.Settings;
  28. websettings.JavaScriptEnabled = true;
  29. //btnGo Click event
  30. btnGo.Click += BtnGo_Click;
  31. }
  32. private void BtnGo_Click(object sender, System.EventArgs e)
  33. {
  34. //Load URL in txtURL in WebView.
  35. webView.LoadUrl(txtURL.Text);
  36. }
  37. }
  38. internal class WebViewClientClass : WebViewClient
  39. {
  40. //Give the host application a chance to take over the control when a new URL is about to be loaded in the current WebView.
  41. public override bool ShouldOverrideUrlLoading(WebView view, string url)
  42. {
  43. view.LoadUrl(url);
  44. return true;
  45. }
  46. }
  47. }
Build and run the Application.

Example of working with navigation in WebView

WebView has several methods and properties, which support navigation like GoForward(), GoBack(), CanGoBack and CanGoForward. For demonstration, we will add Back and Forward button in Main.axml layout.

Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <EditText
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/txtURL" />
  9. <LinearLayout
  10. android:orientation="horizontal"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/linearLayout1">
  14. <Button
  15. android:text="Back"
  16. android:layout_width="wrap_content"
  17. android:layout_height="match_parent"
  18. android:id="@+id/btnBack"
  19. android:layout_weight="1" />
  20. <Button
  21. android:text="Go"
  22. android:layout_width="wrap_content"
  23. android:layout_height="match_parent"
  24. android:id="@+id/btnGO"
  25. android:layout_weight="2" />
  26. <Button
  27. android:text="Forward"
  28. android:layout_width="wrap_content"
  29. android:layout_height="match_parent"
  30. android:id="@+id/btnForward"
  31. android:layout_weight="1" />
  32. </LinearLayout>
  33. <android.webkit.WebView
  34. android:layout_width="match_parent"
  35. android:layout_height="match_parent"
  36. android:id="@+id/webView" />
  37. </LinearLayout>
MainActivity.cs
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4. using Android.Webkit;
  5. using Android.Runtime;
  6. using Android.Views;
  7. namespace WebViewwithNavigation
  8. {
  9. [Activity(Label = "WebViewwithNavigation", MainLauncher = true, Icon = "@drawable/icon",Theme ="@android:style/Theme.NoTitleBar")]
  10. public class MainActivity : Activity
  11. {
  12. Button btnBack;
  13. Button btnGo;
  14. Button btnForward;
  15. TextView txtURL;
  16. WebView webView;
  17. protected override void OnCreate(Bundle bundle)
  18. {
  19. base.OnCreate(bundle);
  20. // Set our view from the "main" layout resource
  21. SetContentView (Resource.Layout.Main);
  22. btnGo = FindViewById<Button>(Resource.Id.btnGO);
  23. btnBack = FindViewById<Button>(Resource.Id.btnBack);
  24. btnForward = FindViewById<Button>(Resource.Id.btnForward);
  25. txtURL = FindViewById<TextView>(Resource.Id.txtURL);
  26. webView = FindViewById<WebView>(Resource.Id.webView);
  27. webView.SetWebViewClient(new WebViewClientClass());
  28. WebSettings websettings = webView.Settings;
  29. websettings.JavaScriptEnabled = true;
  30. btnGo.Click += BtnGo_Click;
  31. btnBack.Click += BtnBack_Click;
  32. btnForward.Click += BtnForward_Click;
  33. }
  34. private void BtnForward_Click(object sender, System.EventArgs e)
  35. {
  36. //If WebView has forward History item then forward to the next visited page.
  37. if (webView.CanGoForward())
  38. {
  39. webView.GoForward();
  40. }
  41. }
  42. private void BtnBack_Click(object sender, System.EventArgs e)
  43. {
  44. //If WebView has back History item then navigate to the last visited page.
  45. if (webView.CanGoBack())
  46. {
  47. webView.GoBack();
  48. }
  49. }
  50. private void BtnGo_Click(object sender, System.EventArgs e)
  51. {
  52. webView.LoadUrl(txtURL.Text);
  53. }
  54. }
  55. internal class WebViewClientClass : WebViewClient
  56. {
  57. public override bool ShouldOverrideUrlLoading(WebView view, string url)
  58. {
  59. view.LoadUrl(url);
  60. return true;
  61. }
  62. }
  63. }

Build and run the Application. Go to any Web URL (In my case, I am visiting http://www.google.com ).

Afterwards, I searched c-sharpcorner in Google and clicked on its official site in order to generate back history items.
After URL opens, click on Back and Forward button in order to navigate from one page to another.
I hope you liked it. Thanks.