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

Boris Kachscovsky

07/07/2021, 11:20 AM
Is there any way to define the width of an element after it had been measured in a custom layout?
Copy code
Layout(content = {
   Composable1()
   Composable2()
   ComposableWhoseSizeDependsOnOtherComposables()
} { measurables, constraints ->
 // I measure all the elements and get the width i'd like the last element to be
 layout(width, height) {
   // I place placables for composable 1 and 2
   //
   // How can i make composable 3's width depend on the measurements I had made on 1 and 2?
 }
}
a

Albert Chang

07/07/2021, 2:27 PM
Use
SubcomposeLayout
.
a

Adam Powell

07/07/2021, 3:08 PM
Not necessarily, this is a case where I would use a parent data modifier to tag composable 3 for different treatment, and handle it as part of the custom layout above. SubcomposeLayout would be overkill here since the only dependent element is width, not composed structure.
👍 1
b

Boris Kachscovsky

07/07/2021, 4:13 PM
Makes sense 👍 I think what i went with is messing with the constraints of the third composable. Something like:
Copy code
val linePlaceable = measurables.last().measure(
                constraints.copy(
                    minWidth = someMeasuredWidth,
                    maxWidth = someMeasuredWidth
                )
            )
Measuring it against those constraints is a nice way to change the size without requiring subcompose layout!
5 Views