Use WebView to deliver a web application or a web page as a part of a client application. The WebView class is an extension of Android's View class that lets you display web pages as a part of your activity layout. It doesn't include the features of a fully developed web browser, such as navigation controls or an address bar. All WebView does, by default, is show a web page.
WebView can help you provide information in your app that you might need to update, such as an end-user agreement or a user guide. Within your Android app, you can create an Activity that contains a WebView, then use it to display your document that's hosted online.
WebView can also help when your app provides data to the user that 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 app 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-powered devices and then implement a WebView in your Android app that loads the web page.
This document describes how to get started with WebView, how to bind JavaScript from your web page to client-side code in your Android app, how to handle page navigation, and how to manage windows when using WebView.
To safely use more-recent WebView capabilities on the device your app is running on, add the AndroidX Webkit library. This is a static library you can add to your application to use android.webkit APIs that aren't available for earlier platform versions.
Add it to your build.gradle file as follows:
// Kotlin build.gradle.kts
dependencies {
implementation("androidx.webkit:webkit:1.8.0")
}
// Groovy build.gradle
dependencies {
implementation ("androidx.webkit:webkit:1.8.0")
}
To add a WebView to your app, you can include the <WebView> element in your activity layout or set the entire Activity window as a WebView in onCreate().
To add a WebView to your app in the layout, add the following code to your activity's layout XML file:
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
To load a web page in the WebView, use loadUrl(), as shown in the following example:
// Kotlin
val myWebView: WebView = findViewById(R.id.webview)
myWebView.loadUrl("http://www.example.com")
// Java
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
Or load the URL from an HTML string:
// Create an unencoded HTML string, then convert the unencoded HTML string into bytes. Encode it with base64 and load the data.
// Kotlin
val unencodedHtml =
"<html><body>'%23' is the percent code for ‘#‘ </body></html>";
val encodedHtml = Base64.encodeToString(unencodedHtml.toByteArray(), Base64.NO_PADDING)
myWebView.loadData(encodedHtml, "text/html", "base64")
// Java
String unencodedHtml =
"<html><body>'%23' is the percent code for ‘#‘ </body></html>";
String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(),
Base64.NO_PADDING);
myWebView.loadData(encodedHtml, "text/html", "base64");
Your app must have access to the internet. To get internet access, request the INTERNET permission in your manifest file, as shown in the following example:
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
You can customize your WebView by doing any of the following:
Enabling fullscreen support using WebChromeClient. This class is also called when a WebView needs permission to alter the host app's UI, such as creating or closing windows or sending JavaScript dialogs to the user. To learn more about debugging in this context, read Debug web apps.
Handling events that impact content rendering, such as errors on form submissions or navigation using WebViewClient. You can also use this subclass to intercept URL loading.
Enabling JavaScript by modifying WebSettings.
Using JavaScript to access Android framework objects that you have injected into a WebView.
If the web page you want to load in your WebView uses JavaScript, you must enable JavaScript for your WebView. After you enable JavaScript, you can create interfaces between your app code and your JavaScript code.
JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView. Retrieve WebSettings with getSettings(), then enable JavaScript with setJavaScriptEnabled().
See the following example:
// Kotlin
val myWebView: WebView = findViewById(R.id.webview)
myWebView.settings.javaScriptEnabled = true
// Java
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);