How to create a custom layout with AndroidView?. F...
# compose
m
How to create a custom layout with AndroidView?. For some reason, the measured height of the AndroidView occupies all height. I checked, and it is the wrapper view that has that size. The real view has it’s size correct. Any help ?
z
What are the layout constraints coming into the AndroidView?
m
match_parent, wrap_content
z
That's the views layout params, what are the compost constraints?
m
fillMaxWidth, wrapContentHeight
it works with a column, it works with constraintLayout. With Layout it does take all space. weird
z
Hm, I'd need to review how these interact bit wrapContentHeight with MATCH_PARENT sounds suspicious. From compose you're saying “just be whatever height you want” and then from the view you're saying “be as tall as possible”. But wrapContentHeight will by default only change the min constraint, not the max. I'm guessing AndroidView is going to use the max constraint for MATCH_PARENT.
m
I feel i am doing sth wrong. But i cannot figure out what.
Copy code
Layout(content = {
    MyAndroidView(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
    )

    Content()
}){ measurables, constraints ->

    val myAndroidViewPlaceable = measurables[0].measure(constraints)
    val contentPlaceable = measurables[1].measure(constraints)

    layout(constraints.maxWidth, constraints.maxHeight){
        myAndroidViewPlaceable.place(
            x = 0,
            y = 0
        )
        contentPlaceable.place(
            x = 0,
            y = myAndroidViewPlaceable.height
        )
    }
}
this should just work right ?
ya, but its wrap_content for the height in the android part
z
Oh yea, my bad. Literally just making my first coffee now 😅
Gimme a minute to see how this measuring is implemented
m
i inspected the layout. The wrapper view that compose creates do take all the height. The real view, which is inside, takes the correct space. that’s what’s weird
z
One other note though, you might want to adjust the constraints for your content to account for the space taken up by the android view.
And if this is your actual code, I think it would be simpler and easier to read intent if instead of using layout modifiers inside your content you just directly manipulated the constraints.
m
makes sense, ya. i’ll try
I think you can reproduce by building a ‘Layout’ with a AndroidView, any view. For some reason, it takes all space
z
What are the constraints your layout is getting?
m
i changed the constraint for the androidview to
constraints.copy(minWidth = 0, minHeight = 0
and it correctly shows
why would this influence the height taken. - hm. i need to study this mechanics better. i dont understand 😛
z
Again, what are the constraints coming into your layout? 😜
m
no constraints. its Scafold content body
z
Every composable is measured with constraints
It’s the value of the
constraints
argument to your layout lambda
I'm wondering if the incoming min height was probably big, and so the wrapContentHeight on your android view was filling all that space. But it's harder to think about with those internal layout modifiers. All they do is modify the constraints on the way down so it's better to just do that yourself if you're already quitting a custom layout anyway
m
ya. i’ll investigate a bit. Thanks for the help