Web View

PDF for offline use
Related APIs:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-07

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"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
  3. 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

  4. 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;
  5. When the WebView is asked to load a URL, it will by default delegate the request to the default browswer. To have the WebVie load the URL instead of the default broweser, it is necessary to subclass Android.Webkit.WebViewClient and override the ShouldOverriderUrlLoading method. An instance of this custom WebViewClient is provided to the WebView. To do this, add this nested class inside MainActivity:

    public class HelloWebViewClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading (WebView view, string url)
        {
            view.LoadUrl (url);
            return false;
        }
    }

    When ShouldOverrideUrlLoading returns false, it signs Android that the current WebView instance handled the request and that no further action is necessary.

  6. 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.SetWebViewClient(new HelloWebViewClient());
        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).

  7. 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")]
  8. Now run the application. You should now have a simple web page viewer.

  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.