https://kotlinlang.org logo
#compose
Title
# compose
c

Colton Idle

10/16/2023, 3:10 PM
Is weight the best way to do "percentage based layouts" in compose? It seems to do what I want... but I know that weights distributes stuff at a second "pass" so I'm not sure if that will necessarily always get me the size I was going for.
"parent will divide the vertical space remaining after measuring"
All I want to do is something like
Copy code
Column(fillMaxHeight){
Box(height(percent = 50))
Box(height(percent = 50))
}
c

cah

10/16/2023, 3:17 PM
fillMaxHeight takes a float that will do this Box(Modifier.fillMaxHeight(0.5f))
i

Ian Lake

10/16/2023, 3:36 PM
And it isn't a second pass - it just changes the measurement order to measure non-weighted items first, then distribute the rest of the space to weighted items
1
c

Colton Idle

10/16/2023, 4:14 PM
@cah if I do Box(Modifier.fillMaxHeight(0.5f)) Box(Modifier.fillMaxHeight(0.5f)) then the second box takes up 25% of the available space leaving me with 25% left of free space
@Ian Lake interesting. so maybe i dont have to worry about a condition where having weight 1f and another weight 1f (like my example above) and it somehow being like a 60/40 split.
i

Ian Lake

10/16/2023, 4:19 PM
Distribution of the remaining space for weighted items is a fraction - weight / (sum of all weights) so it doesn't matter if you use 0.5f for both or 1f for both since they're the same fraction of the sum
c

Colton Idle

10/16/2023, 4:21 PM
understood. my "worry" was more "could there ever be a way to possibly have an uneven split by doing"
Copy code
Column(fillMaxHeight){
Box(weight(1f))
Box(weight(1f))
}
just because i was worried about the whole "parent will divide the vertical space remaining after measuring" So like. if it goes to layout the first box and it takes up 60% of the space because it has a bunch of tall content (idk, like a lot of text?) would that mean that you'd get less space in the second box.
But I think your point of "And it isn't a second pass - it just changes the measurement order to measure non-weighted items first, then distribute the rest of the space to weighted items" basically guarantees that wouldn't happen. non weighted items go first (in my case i dont have any non-weighted) and then distribute the weight to the items with weights. so i guess id always get 50/50 split. effectively percent based layouts
i

Ian Lake

10/16/2023, 4:26 PM
Yep, that's the key benefit of using weights
c

Colton Idle

10/16/2023, 4:31 PM
Cool. I think I was misunderstanding when weights are applied etc. Thanks for teaching!