When I’m trying to use a custom layout on a compos...
# compose
m
When I’m trying to use a custom layout on a composable that contains content, i always get an exception. This is just an example, but it shows my issue. I guess im doing something completely wrong here.
Copy code
@Composable
fun Test(
	content: @Composable () -> Unit
) {
	Box(
		Modifier.layout { measurable, constraints ->
			layout(constraints.minWidth, constraints.minHeight){
				val placeable = measurable.measure(constraints)
				placeable.place(0,0)
			}
		}
	) {
		content()
	}
}

@Preview
@Composable
fun TestPreview() {
	Test(){
		Text(text = "test")
	}
}
c
Can you share the error? I guess you have to measure the measurable outside the layout block
m
Copy code
ava.lang.IllegalStateException: Measurable could be only measured from the parent's measure or layout block.Parents state is NeedsRelayout
since the modifier only works on one measurable, i guess there is no way to actually override the behaviour for a component that has content [components]?
c
Measurables usually is a list. So you should use for each and move it outside the layout block. So first let the “view” calculate how much space they want do have and then use that information to lay them out.
m
the Modifier.layout only gives one item back (as far as I understand it). For a full Layout component I get a list instead
c
ah, I see. I have never used the modifier. did you follow this https://developer.android.com/jetpack/compose/layouts/custom#layout-modifier the
measure
is still outside the
layout
m
your right, I’ll check
Thanks.
c
Welcome 🙂