Web View

PDF for offline use

Let us know how you feel about this

Translation Quality


last updated: 2017-06

WebView allows you to create your own window for viewing web pages (or even develop a complete browser). In this tutorial, you'll create a simple Activity that can view and navigate web pages.

  1. Create a new project named HelloWebView.

  2. Open Resources/Layout/Main.axml and insert the following:

    <?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
  3. Now open MainActivity.cs and add a using directive for Webkit:

    using Android.Webkit;

    At the top of the class, declare a WebView object:

    WebView web_view;

    Next, use the following code for the OnCreate() method:

    protected override void OnCreate (Bundle bundle)
        base.OnCreate (bundle);
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        web_view = FindViewById<WebView> (Resource.Id.webview);
        web_view.Settings.JavaScriptEnabled = true;
        web_view.LoadUrl ("http://www.google.com");

    This initializes the member WebView with the one from the Activity layout and enables JavaScript for the WebView with JavaScriptEnabled = true (see the Call C# from JavaScript recipe for information about how to call C# functions from JavaScript). Finally, an initial web page is loaded with LoadUrl(String).

  4. Because this application needs access to the Internet, you need to add the appropriate permissions to the Android manifest file. Open your project's properties to specify which permissions your application requires to operate. For our web browser, we need the INTERNET permission to access the internet:

    Setting the INTERNET permission in the Android Manifest

  5. You can give some more space for web pages by removing the title bar, using the NoTitleBar theme specified in the [Activity] attribute:

    [Activity (Label = "Web View Sample", MainLauncher = true, Theme = "@android:style/Theme.NoTitleBar")]
  6. Now run the application. You should now have a simple web page viewer. It's not quite a browser yet because as soon as you click a link, the default Android Browser handles the Intent to view a web page, because this Activity isn't technically enabled to do so. Instead of adding an intent filter to view web pages, you can subclass WebViewClient and enable the Activity to handle its own URL requests.

  7. In the HelloAndroid Activity, add this nested class:

    public class HelloWebViewClient : WebViewClient
        public override bool ShouldOverrideUrlLoading (WebView view, string url)
            view.LoadUrl (url);
            return true;
  8. Then towards the end of the OnCreate(Bundle) method, set an instance of the HelloWebViewClient as the WebViewClient:

    web_view.SetWebViewClient (new HelloWebViewClient ());

    This line can go anywhere following the initialization of the WebView object.

    This creates a WebViewClient that will load any URL selected from this WebView into the same WebView. The ShouldOverrideUrlLoading(WebView, String) method is passed the current WebView and the URL requested, so all it needs to do is load the URL in the given view. Returning true says that the method has handled the URL and the event should not propagate (in which case, an Intent would be created that's handled by the Browser application).

    If you run the application again, new pages will now load in this Activity. However, you can't navigate back to previous pages. To do this, you need to handle the BACK button on the device, so that it will return to the previous page, rather than exit the application.

  9. To handle the BACK button key press, add the following method inside the HelloWebView Activity:

    public override bool OnKeyDown (Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
        if (keyCode == Keycode.Back && web_view.CanGoBack ()) 
            web_view.GoBack ();
            return true;
        return base.OnKeyDown (keyCode, e);

    This OnKeyDown(int, KeyEvent) callback method will be called anytime a button is pressed while in the Activity. The condition inside uses the KeyEvent to check whether the key pressed is the BACK button and whether the WebView is actually capable of navigating back (if it has a history). If both are true, then the GoBack() method is called, which will navigate back one step in the WebView history.Returning true indicates that the event has been handled. If this condition is not met, then the event is sent back to the system.

  10. Run the application again. You'll now be able to follow links and navigate back through the page history.

When you open the application, it should look like this:

Screenshot of WebView opened to Google's search page


Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.