androidlayoutwebviewandroid-jetpack-composelayoutparams

How to apply AndroidView webview size to ModalBottomSheetLayout in jetpack compose


I have ModalBottomSheetLayout and its loading webview on it.

But ModalBottomSheetLayout shows only part of the webview content.

How can I change ModalBottomSheetLayout by the size of loaded content on webview?

On the ModalBottomSheetLayout will be only one webview.

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomSheetScreen(url: String, content: @Composable (ModalBottomSheetState) -> Unit) {
    val bottomSheetState =
        rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden,
            animationSpec = tween(
                durationMillis = 500,
                delayMillis = 0,
                easing = FastOutLinearInEasing
            ),
            confirmValueChange = {
                true
            })

    ModalBottomSheetLayout(
        sheetState = bottomSheetState,
        sheetShape = RoundedCornerShape(18.dp),
        sheetContent = {
            Column() {
                WebView(url = url)
            }
        },
    ) {
        content(bottomSheetState)
    }
}

I don't know size of webview content.

@SuppressLint("SetJavaScriptEnabled")
@Composable
fun WebView(url: String) {
    AndroidView(factory = {
        WebView(it).apply {
            layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
            settings.builtInZoomControls = true
            settings.javaScriptEnabled = true
            settings.setSupportZoom(true)
            settings.userAgentString
            webChromeClient = WebChromeClient()
            //webViewClient = object : WebChromeClient() {}

            webViewClient = object : WebViewClient() {
                override fun onPageFinished(view: WebView, url: String) {
                }
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    return true
                }
                override fun onReceivedError(
                    view: WebView?,
                    request: WebResourceRequest?,
                    error: WebResourceError?
                ) {
                }
            }
            loadUrl(url)

        }
    }, update = {
        //it.loadUrl(url)
       // it.layoutParams.height = it.height
    })
}

Solution

  • found only such solution

    @Composable
    fun WebView(url: String, onClose: () -> Unit) {
        val state = rememberWebViewState(url)
        val context = LocalContext.current
        val webViewClient = remember {
            object : AccompanistWebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    return true
                }
            }
        }
        val chromeViewClient = remember {
            object : AccompanistWebChromeClient() {
                override fun onCloseWindow(window: WebView?) {
                    super.onCloseWindow(window)
                }
            }
        }
        WebView(
            state = state,
            onCreated = { webView ->
                webView.settings.javaScriptEnabled = true
            },
            client = remember { webViewClient },
            chromeClient = remember { chromeViewClient }
        )
    }