Web View
- PDF for offline use:
Let us know how you feel about this.
0/250
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.
- Create a new project named HelloWebView.
- Open the Resources
\Layout\Main.axmlfile 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" /> Now open the Activity1
.csfile. Add a using directive for Webkit:using Android.Webkit;At the top of the class, declare a
WebViewobject:WebView web_view;Then 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
WebViewwith the one from theActivitylayout and enables JavaScript for theWebViewwithJavaScriptEnabled= 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 withLoadUrl(String).- Because this application needs access to the Internet, you need to add
the appropriate permissions to the Android manifest file. Bring up your
project's properties with Project -> HelloWebView Properties. This can be done is Xamarin Studio by right clicking on the project and going to Options -> Android Application. Since
there is no default manifest file, click the link to add one:

Now that you have a manifest, you can specify which permissions your application requires to operate. For our web browser, we need the INTERNET permission to access the internet:
- 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")] - Now run the application.
You now have a simplest 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
Activityisn't technically enabled to do so. Instead of adding an intent filter to view web pages, you can override the<a href= "http://androidapi.xamarin.com/?link=T%3aAndroid.Webkit.WebViewClient" target="_blank">WebViewClient</a>class and enable thisActivityto handle its own URL requests. - In the
HelloAndroidActivity, add this nested class:public class HelloWebViewClient : WebViewClient { public override bool ShouldOverrideUrlLoading (WebView view, string url) { view.LoadUrl (url); return true; } } - Then towards the end of the
OnCreate(Bundle)method, set an instance of theHelloWebViewClientas theWebViewClient:web_view.SetWebViewClient (new HelloWebViewClient ());This line can go anywhere following the initialization of the
WebViewobject.This creates a
WebViewClientthat will load any URL selected from thisWebViewinto the sameWebView. TheShouldOverrideUrlLoading(WebView, String)method is passed the currentWebViewand the URL requested, so all it needs to do is load the URL in the given view. Returningtruesays 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.
- To handle the BACK button key press, add the following method inside the
HelloWebViewActivity: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 theKeyEventto check whether the key pressed is the BACK button and whether theWebViewis actually capable of navigating back (if it has a history). If both are true, then theGoBack()method is called, which will navigate back one step in theWebViewhistory.Returningtrueindicates that the event has been handled. If this condition is not met, then the event is sent back to the system. - 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:
Resource
WebViewWebViewClient-
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 . This tutorial is based on the Android Web View tutorial .
Let us know how you feel about this.
0/250
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.


