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.
Create a new project named HelloWebView.
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" />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
INTERNETpermission to access the internet:Now open MainActivity.cs and add a using directive for Webkit:
using Android.Webkit;At the top of the class, declare a
WebViewobject:WebView web_view;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.WebViewClientand override theShouldOverriderUrlLoadingmethod. An instance of this customWebViewClientis provided to theWebView. To do this, add this nested class insideMainActivity:public class HelloWebViewClient : WebViewClient { public override bool ShouldOverrideUrlLoading (WebView view, string url) { view.LoadUrl (url); return false; } }When
ShouldOverrideUrlLoadingreturnsfalse, it signs Android that the currentWebViewinstance handled the request and that no further action is necessary.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
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).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 should now have a simple web page viewer.
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:
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.
Let us know how you feel about this
Translation Quality
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.



