Hello, I'm trying to be responsive in my designs a...
# compose
n
Hello, I'm trying to be responsive in my designs and trying to display either 4 items in a
Row
(if they all fit horizontaly with 25% width each) or either 2 items (filling 50% width each) by row. I see that you cannot measure twice in
Layout.measurePolicy
, so the only way I see is giving twice the composables to my Layout so it can measure the "first one" (with 25% width constraints), and if I don't have enought space, measure the "second one" with the 50% width constraint). It completely defeats the purpose of "avoid exponential layout calculation" (the main reason why we can't measure twice if I understand) but I don't see any other way. Any ideas? 💡 PS: I checked FlowRow but it's basically useless, it needs a fixed number of items per Row, which is dynamic in my case (2 or 4)
t
Can't you just have a
BoxWithContraints
, see the available
width
and then decide between 2 or 4 and use a Grid or something like that?
n
The width of each item can widely change unfortunately, so
BoxWithContraints.maxWidth
isn't enough
z
You typically use intrinsics to do stuff like this. Intrinsics are like a more constrained version of measurement that can be queried before doing the real measure. So for this you'd write a custom layout that would ask all your items for their min intrinsic width and use those to determine if they'll fit.
☝️ 1
👀 1
a
This may not work for your use case, but you could get clever with the structuring for
FlowRow
to work - if you put each pair of items into a
Row
, you could maybe get into a case where you have either 1 or 2 "pairs" per row.
1
🧠 1
Otherwise a custom layout is the main super flexible tool - they're not too difficult to write.
n
I went the
Layout
+
maxIntrinsicWidth
way, thank you all for your help! Here's the result: https://gist.github.com/NinoDLC/9770237de84071e18e8f1c86ef60e03c I'm not sure if I should override others functions than
MultiContentMeasurePolicy.IntrinsicMeasureScope.minIntrinsicWidth
but for now it works. Thanks again! PS: Good idea Alex but I didn't mention there was separators between items so it could not work in this case. But I'll def keep it in mind when separators are not needed 😄
Capture d'écran 2025-09-23 135046.png
t
Nice names 😆
n
I needed sample data that wasn't company private 😂
😂 1
z
I would test with text content that can line break, since that’s where min and max intrinsic will differ. Min will be the width of the widest line when every opportunity to break is taken (ie the widest word, basically), and max will be the width if everything is left on a single line.
And you shouldn’t need to override any of the intrinsics methods in your measure policy, the default implementation will delegate to your main measure method.