Skip to content

WebView wrapper for Jetpack Compose

Maven Central

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

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
)

Download

Maven Central

repositories {
    mavenCentral()
}

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