james
03/15/2022, 12:45 AMWebView in an AndroidView for use in a compose app, I have need of methods on the webview instance outside of the wrapped component for example a refresh button that calls webView.loadUrl(…). How do I expose this to composable functions using the WebView component? Unsure how to expose this from the wrapped componentTash
03/15/2022, 2:17 AM@Composable
fun WebViewWrapper(url: String?, modifier: Modifier = Modifier) {
AndroidView(
factory = { context ->
android.webkit.WebView(context).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
// set up webview
if (url != null) loadUrl(url)
}
},
update = { innerWebView ->
if (url != null) {
innerWebView.loadUrl(url)
}
}
)
}
basically converting the imperative webView.loadUrl() into a declarative/state-based functionalityTash
03/15/2022, 2:18 AMTash
03/15/2022, 2:38 AMwebView.reload / goForward/Backward etcjames
03/15/2022, 3:39 AMWebViewWrapper composable in a Column with a button that called webView.loadUrl() in onClick do you have any examples on how you would expose the WebView instance to onClick in that scenario? I had solved this by keeping a reference to WebView in my viewmodel, however this feels wrong 😅james
03/15/2022, 3:40 AM@Composable
fun SomeColumn() {
WebViewWrapper()
Button(onClick = { ^trigger WebView.loadUrl() somehow }
}james
03/15/2022, 4:08 AMColton Idle
03/16/2022, 4:11 AM