Webview in Compose Android
2 min readJul 31, 2023
In this blog we will learn how to create webview with Jetpack Compose. You can view webpages and can send intent to external browsers to load web url.
In-App Webview
In Compose there isn’t dedicated component for webview. Webview is part of the android view itself.
Webview can be defined like as the example shown below.
@Composable
private fun WebviewScreen(url: String) = AndroidView(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
factory = {
WebView(it).apply {
webViewClient = WebViewClient()
loadUrl(url)
}
})
This view takes in the URL provided as function parameter and loads the webview with the given URL.
Output
Webview via Intent
If you want view to be redirected to browser with Intent then following can be done.
@Composable
private fun WebViewScreen(url: String, context: Context) =
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
Output
That’s all for now. Thanks.
Please feel free to reach out to me on LinkedIn.