Search code examples
androidkotlinodooandroid-jetpack-compose

Opening Odoo webpages (Main page or clients page) with Android WebView (Compose)


I'm trying to open odoo website, using Android WebView. but I always get an empty page. no errors or warnings, other pages opens with no problem. but it seems that there are specific settings in WebView for Odoo page. I created the same application for an iOS and it opened with no issue. I made this code


class MainActivity : ComponentActivity() {
    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AndroidView(
                factory = { context ->
                    WebView(context).apply {
                        webViewClient = WebViewClient()
                        settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
                        settings.javaScriptEnabled = true
                        settings.domStorageEnabled = true
                        settings.loadWithOverviewMode = true
                        settings.useWideViewPort = true
                        settings.allowContentAccess = true
                        settings.allowFileAccess = true
                        settings.databaseEnabled = true
                        settings.loadsImagesAutomatically = true
                        settings.javaScriptCanOpenWindowsAutomatically = true
                        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
                        isScrollbarFadingEnabled = true
                    }
                },
                update = {
                    it.loadUrl("https://www.odoo.com/")
                }
            )
        }
    }
}

what are the right settings?


Solution

  • It was simple. but tricky. I had to load the data for the WebView to load Odoo website.

    update = {
                        val s = "<!DOCTYPE html><html><head><style>html, body { margin: 0; padding: 0; height: 100%; }</style></head><body><div style='height: 100%; background-color: white;'></div></body></html>"
                        it.loadData(s, "text/html", "UTF-8")
                        it.loadUrl("https://www.odoo.com/")
                    }
    

    here is the complete code. I may not need all of that, but someone might find this useful.

    class MainActivity : ComponentActivity() {
        @SuppressLint("SetJavaScriptEnabled")
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContent {
                AndroidView(
                    factory = { context ->
                        WebView(context).apply {
                            layoutParams = ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT)
                            webViewClient = WebViewClient()
                            settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
                            settings.javaScriptEnabled = true
                            settings.domStorageEnabled = true
                            settings.loadWithOverviewMode = true
                            settings.useWideViewPort = true
                            settings.allowContentAccess = true
                            settings.allowFileAccess = true
                            settings.databaseEnabled = true
                            settings.loadsImagesAutomatically = true
                            settings.javaScriptCanOpenWindowsAutomatically = true
                            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
                            settings.offscreenPreRaster = true
                            settings.cacheMode = WebSettings.LOAD_DEFAULT
                            isScrollbarFadingEnabled = true
                        }
                    },
                    update = {
                        val s = "<!DOCTYPE html><html><head><style>html, body { margin: 0; padding: 0; height: 100%; }</style></head><body><div style='height: 100%; background-color: white;'></div></body></html>"
                        it.loadData(s, "text/html", "UTF-8")
                        it.loadUrl("https://www.odoo.com/")
                    }
                )
            }
        }
    }