6

I'm facing an issue with the WebView in my Android Compose application. While the WebView works perfectly on iOS, in the browser, and with Flutter WebView on both Android and iOS, certain websites break and become unresponsive when using the WebView with Compose and Kotlin.

Here's the code I'm using:

@SuppressLint("SetJavaScriptEnabled")
@Composable
internal fun WebView(downloadListener: SDKDownloadListener, modifier: Modifier = Modifier) {
    var showSplash by remember { mutableStateOf(true) }

    Box(modifier = modifier) {
        AndroidView(
            factory = { context ->
                WebView(context).apply {
                    // Clear various caches and preferences
                    clearCache(true)

                    // Configure WebView settings
                    settings.apply {
                        javaScriptEnabled = true
                        domStorageEnabled = true
                        databaseEnabled = true
                        allowContentAccess = true
                        allowFileAccess = true
                    }

                    // Set WebView properties
                    webViewClient = WebViewClient()
                    webChromeClient = WebChromeClient()

                    // Set download listener
                    setDownloadListener(downloadListener)

                    // Add JavaScript interface
                    addJavascriptInterface(EventListenerJavascriptInterface(), JAVASCRIPT_INTERFACE)

                    // Load URL with POST data
                    postUrl("https://post.dev", body)
                }
            },
            modifier = Modifier.matchParentSize(),
        )

        // Splash screen with animation
        AnimatedVisibility(
            visible = showSplash,
            exit = slideOutVertically(targetOffsetY = { it }) + fadeOut(),
            modifier = Modifier.matchParentSize()
        ) {
            SplashView(onButtonPress = { showSplash = false })
        }
    }
}

Tried:

  1. Clearing cache, setting javascript enabled, enabling database etc.
  2. Checking for any JavaScript errors on the websites.

Despite these efforts, certain websites still break and are unresponsive. Has anyone encountered a similar issue or can provide insights into what might be going wrong? Any help would be appreciated!

Additional Information: The same websites work fine on iOS and in the browser. Flutter WebView works perfectly on both Android and iOS with the same websites.

4
  • yes im having exact same problem atm in a jetpack compose app (a login webpage not displaying correctly, but renders fine in say chrome). would love to know how to solve this too Commented May 15, 2024 at 13:23
  • @ajonno it seems that there's some issue with using WebView on Compose. I ended up porting my code to Kotlin + XML layouts Commented May 16, 2024 at 15:29
  • I solved mine by adding layoutParams. Tried to post this as an answer, but the mod deleted it. Commented Nov 18, 2024 at 1:38
  • The solution from @Ilya-gazman works. That should be accepted answer. Commented May 27 at 8:58

2 Answers 2

10

Yep, had the same issue, couldn't load local js files. The solution is to wrap it with a FrameLayout

val myWebView = remember {
     WebView(context).apply{
           //your settings here
      }
}
AndroidView(factory = {
    FrameLayout(it).apply {
        if (myWebView.parent != null) {
            (myWebView.parent as ViewGroup).removeView(myWebView)
        }
        addView(myWebView)
    }
}, modifier = Modifier.fillMaxSize())
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! that's saved lot of hours and still don't know why the only compose didn't worked in certain cases btw this works like magic :)
Thanks man! Five hours of problem solving that led nowhere. How did you arrive at this solution?
I spent 10 hours of my own ;)
0

I think Webview doesn't work properly with just compose. You should use WebView in XML layout and inflate it in Compose function

  1. Create xml layout

  2. Open View Binding in module Gradle

    buildFeatures { viewBinding = true }

  3. Load XML with View Binding

       Column(
         // Load WebView from XML Layout without ViewBinding
         AndroidViewBinding(WebviewViewBinding::inflate) {
             setupWebView(this.webview)
             }
       }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.