Hello Web View

A WebView allows you to create your own web browser Activity. In this tutorial, we'll create a simple Activity that can view web pages.

1. Create a new project/Activity called HelloWebView.

2. Open the layout file. Insert a WebView so it looks like so:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout width="wrap content" android:layout height="wrap content" android:orientation="vertical"> <WebView android:id="@+id/webview" android:layout width="fill parent" android:layout height="fill parent"

</LinearLayout>

3. Now open the HelloWebView.java file. At the top of the class, instantiate a WebView object:

WebView webview;

Then add the following at the end of the onCreate() method:

webview = (WebView) findViewByld(R.id.webview);

webview.getSettings().setJavaScriptEnabled(true);

webview.loadUrl("http://www.google.com");

This captures the WebView we created in our layout, then requests a WebSettings object and enables JavaScript. Then we load a URL. 4. Because we're accessing the internet, we need to add the appropriate permissions to the Android manifest file. So open the AndroidManifest.xml file and, add the following as a child of the<manifest> element:

<uses-permission android:name="android.permission.INTERNET" />

You now have the world's simplest web page viewer. It's not quite a browser yet. It only loads the page we've requested.

We can load a page, but as soon as we click a link, the default Android web browser handles the Intent, instead of our own WebView handling the action. So now we'll override the WebViewClient to enable us to handle our own URL loading.

1. In the HelloAndroid Activity, add this nested private class:

private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true;

2. Now, in the onCreate() method, set an instance of the HelloWebViewClient as our WebViewClient:

webview.setWebViewClient(new WebViewClientDemo());

This line should immediately follow the initialization of our WebView object.

What we've done is create a WebViewClient that will load any URL selected in our WebView in the same WebView. You can see this in the shouldOverrideUrlLoading() method, above—it is passed the current WebView and the URL, so all we do is load the URL in the given view. Returning true says that we've handled the URL ourselves and the event should not bubble-up.

If you try it again, new pages will now load in the HelloWebView Activity. However, you'll notice that we can't navigate back. We need to handle the back button on the device, so that it will return to the previous page, rather than exit the application. 3. To handle the back button key press, add the following method inside the HelloWebView Activity:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {

if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { webview.goBack(); return true;

return super.onKeyDown(keyCode, event);

The condition uses a 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 not true, then we send the event up the chain (and the Activity will close). But if both are true, then we call goBack() , which will navigate back one step in the history. We then return true to indicate that we've handled the event.

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

Home Grnail Calendar Redder More

Resource

• WebViewClient

GotJgle

Web [mages Local Mews

Settings Terms view Google in: Mafrite | £2003 Google

There are plenty more Views and widgets available. See the View class for more on View layouts, and the widget package for more useful widgets. And for more raw code samples, visit the Api Demos. These can also be found offline, in /<sdk>/samples/ApiDemos.

Hello, L10N

In this tutorial, we will create a Hello, L10N application that uses the Android framework to selectively load resources. Then we will localize the application by adding resources to the res/ directory.

This tutorial uses the practices described in the Localization document.

0 0

Post a comment