Question about AndroidView + LayoutParams… Should ...
# compose
r
Question about AndroidView + LayoutParams… Should AndroidView give it views a MATCH_PARENT LayoutParams by default? That’s technically how its view will be measured/layout anyways, right? I’ve seen it mentioned that the LayoutParams for the view generated by the factory shouldn’t matter since it’s measured by AndroidView’s measurement rules… But for WebView I think it’s a different story. A WRAP_CONTENT height actually affects the rendering of a WebView. In the docs for WebView, you’re also recommended to use a specific height or MATCH_PARENT height. I’ve seen this in practice, rendering of my WebView in Compose isn’t good unless I set a LayoutParams with MATCH_PARENT.
a
The Modifier passed to AndroidView plus the min/max constraints during measurement should be sufficient such that LayoutParams on the view returned from the factory are effectively ignored/redundant. Do you have a repro case where it's behaving otherwise?
r
I’ll try making a simple demo. In my app, simply setting the WebView’s layoutParams to MATCH_PARENT/MATCH_PARENT during the AndroidView factory had a real difference in rendering.
My guess is some WebView implementation actually changes its rendering based on its own layoutParams height
a
Modifier.fillMax[Width|Height|Size]()
on the
AndroidView
composable should be equivalent to the advice suggested around
MATCH_PARENT
r
To be clear, the WebView is laid out in the same space each time, but it renders its content differently
a
It's possible, we've spent a decade asking people not to do that but there's definitely some code out there that does
If that's what's happening here maybe we just have AndroidView set MATCH_PARENT on the returned view's params internally, since that would be semantically correct as far as the view can tell anyway
r
Yeah that’s what I thought too -- the view created by the factory is stored in an AndroidViewHolder ViewGroup, and it should be matching the AndroidViewHolder’s width/height… so a match_parent layoutparams seems more accurate
with match_parent and without
Copy code
@Composable
fun Test() {
    val context = LocalContext.current
    AndroidView({
        WebView(context).apply {
//            layoutParams = ViewGroup.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.MATCH_PARENT
//            )
            settings.domStorageEnabled = true
            settings.javaScriptEnabled = true
            webViewClient = WebViewClient()
            webChromeClient = WebChromeClient()
            loadUrl("<https://www.google.com/>")
        }
    })
}

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Test()
                }
            }
        }
    }
}
a
Can you file an issue on the tracker please?
r
For sure! Thanks for the quick replies, by the way!
👍 1
a
thanks, getting this triaged to the right place
h
@Johnny fyi