if you want to deliver a web application (or just a web page) as a part of a client application,
you can do it using
WebView
. The
WebView
class is an
extension of Android's
View
class that allows you to display web pages as a
part of your activity layout. It does
not include any features of a fully developed web
browser, such as navigation controls or an address bar. All that
WebView
does, by default, is show a web page.
A common scenario in which using
WebView
is helpful is when you want to
provide information in your application that you might need to update, such as an end-user agreement
or a user guide. Within your Android application, you can create and
Activity
that contains a
WebView
, then use that to display your document that's
hosted online.
Another scenario in which
WebView
can help is if your application provides
data to the user that
always requires an Internet connection to retrieve data, such as email. In this case, you might
find that it's easier to build a
WebView
in your Android application that
shows a web page with all
the user data, rather than performing a network request, then parsing the data and rendering it in
an Android layout. Instead, you can design a web page that's tailored for Android devices
and then implement a
WebView
in your Android application that loads the web
page.
This document shows you how to get started with
WebView
and how to do some
additional things, such as handle page navigation and bind JavaScript from your web page to
client-side code in your Android application.
Adding a WebView to Your Application
To add a
WebView
to your Application, simply include the
<WebView>
element in your activity layout.
For example, here's a layout file in which the
WebView
fills the screen:
<?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"
/>
To load a web page in the
WebView
, use
loadUrl()
. For example:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
Before this will work, however, your application must have access to the Internet. To get
Internet access, request the
INTERNET
permission in your
manifest file. For example:
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...</manifest>
Using JavaScript in WebView
If the web page you plan to load in your
WebView
use JavaScript, you
must enable JavaScript for your
WebView
. Once JavaScript is enabled, you can
also create interfaces between your application code and your JavaScript code.
Enabling JavaScript
JavaScript is disabled in a
WebView
by default. You can enable it
through the
WebSettings
attached to your
WebView
. You can retrieve
WebSettings
with
getSettings()
, then enable
JavaScript with
setJavaScriptEnabled()
.
For example:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebSettings
provides access to a variety of other settings that you might
find useful. For example, if you're developing a web application
that's designed specifically for the
WebView
in your Android application,
then you can define a
custom user agent string with
setUserAgentString()
, then query the custom user agent in your web page to verify that the
client requesting your web page is actually your Android application.