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

Mayank Saini

12/10/2020, 9:46 AM
I have recently upgraded to compose
alpha08
I have encountered this error in
layout
while measuring height.
java.lang.IllegalArgumentException: maxHeight(-35) must be >= minHeight(0)
This is the code snippet.
Copy code
SubcomposeLayout(modifier = modifier) { constraints ->
        val layoutWidth = constraints.maxWidth
        val layoutHeight = constraints.maxHeight
        val gridTopOffset = 10.dp.toPx().toInt()
        val looseConstraints = constraints.copy(minWidth = 0, minHeight = 0)
        layout(width = layoutWidth, height = layoutHeight) {
            val imagePlaceables = subcompose(GamesCompositionContent.Image, image).fastMap {
                it.measure(looseConstraints.copy(minHeight = layoutHeight))
            }
            val imageWidth = imagePlaceables.fastMaxBy { it.width }?.width ?: 0

            val gridPlaceables = subcompose(GamesCompositionContent.GamesGrid) {
                val innerPadding = PaddingValues(
                    start = if (imageWidth != 0) (imageWidth / 2).toDp() else 0.dp
                )
                grid(innerPadding)
            }.fastMap {
                it.measure(
                    looseConstraints.copy(
                        maxWidth = layoutWidth - imageWidth / 2,
                        maxHeight = layoutHeight - gridTopOffset
                    )
                )
            }
The code was working perfectly and no crashes on
alpha07
.
j

jim

12/10/2020, 4:33 PM
cc @George Mount
g

George Mount

12/10/2020, 4:56 PM
It looks like it comes from this line:
Copy code
looseConstraints.copy(
  maxWidth = layoutWidth - imageWidth / 2,
  maxHeight = layoutHeight - gridTopOffset
)
Your
gridTopOffset
is likely larger than your
layoutHeight
m

Mayank Saini

12/11/2020, 3:20 AM
@George Mount Exactly this is the line where it crashes, and it was working perfectly in
alpha07
. What can be the solution here?
g

George Mount

12/11/2020, 4:42 PM
The check isn't new, so I'm guessing that you're being measured with a
0
height for some reason. The solution is to ensure your maxes are within range:
Copy code
looseConstraints.copy(
  maxWidth = (layoutWidth - imageWidth / 2).coerceAtLeast(looseConstraints.minWidth),
  maxHeight = (layoutHeight - gridTopOffset).coerceAtLeast(looseConstraints.minHeight)
)
8 Views