https://kotlinlang.org logo
Title
m

myanmarking

02/06/2022, 7:32 PM
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

Zach Klippenstein (he/him) [MOD]

02/07/2022, 2:22 AM
What are the layout constraints coming into the AndroidView?
m

myanmarking

02/07/2022, 3:58 PM
match_parent, wrap_content
z

Zach Klippenstein (he/him) [MOD]

02/07/2022, 4:29 PM
That's the views layout params, what are the compost constraints?
m

myanmarking

02/07/2022, 4:30 PM
fillMaxWidth, wrapContentHeight
it works with a column, it works with constraintLayout. With Layout it does take all space. weird
z

Zach Klippenstein (he/him) [MOD]

02/07/2022, 4:33 PM
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

myanmarking

02/07/2022, 4:33 PM
I feel i am doing sth wrong. But i cannot figure out what.
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

Zach Klippenstein (he/him) [MOD]

02/07/2022, 4:36 PM
Oh yea, my bad. Literally just making my first coffee now 😅
Gimme a minute to see how this measuring is implemented
m

myanmarking

02/07/2022, 4:37 PM
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

Zach Klippenstein (he/him) [MOD]

02/07/2022, 4:38 PM
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

myanmarking

02/07/2022, 4:41 PM
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

Zach Klippenstein (he/him) [MOD]

02/07/2022, 4:50 PM
What are the constraints your layout is getting?
m

myanmarking

02/07/2022, 4:50 PM
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

Zach Klippenstein (he/him) [MOD]

02/07/2022, 4:55 PM
Again, what are the constraints coming into your layout? 😜
m

myanmarking

02/07/2022, 4:57 PM
no constraints. its Scafold content body
z

Zach Klippenstein (he/him) [MOD]

02/07/2022, 5:01 PM
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

myanmarking

02/07/2022, 5:08 PM
ya. i’ll investigate a bit. Thanks for the help