Skip to content

WebView wrapper for Jetpack Compose

Maven Central

A library which provides a Jetpack Compose wrapper around Android's WebView.

Warning

This library is deprecated, and the API is no longer maintained. We recommend forking the implementation and customising it to your needs. The original documentation is below.

Usage

To implement this wrapper there are two key APIs which are needed: WebView, which is provides the layout, and rememberWebViewState(url) which provides some remembered state including the URL to display.

The basic usage is as follows:

val state = rememberWebViewState("https://example.com")

WebView(
    state
)

This will display a WebView in your Compose layout that shows the URL provided.

There is a larger sample in the sample app which can be found here. This sample also shows how to show a loading state.

WebView settings including JavaScript

By default, JavaScript is disabled in the WebView. To enable it or any other settings you can use the onCreated callback.

WebView(
    state = webViewState,
    onCreated = { it.settings.javaScriptEnabled = true }
)

Capturing back presses

By default the WebView will capture back presses/swipes when relevant and navigate the WebView back. This can be disabled via the parameter on the Composable.

WebView(
    ...
    captureBackPresses = false
)

Using a subclass of WebView

If you want to use a subclass of WebView, or simply require more control over its instantiation, you can provide a factory.

WebView(
    ...
    factory = { context -> CustomWebView(context) }
)

Download

Maven Central

repositories {
    mavenCentral()
}

dependencies {
    implementation "com.google.accompanist:accompanist-webview:<version>"
}