SUBSCRIBE NOW
I always learn something just by skimming it that makes me want to bookmark the issue now and dig deeper later
SUBSCRIBE NOW
Keep up the good work with the newsletter 💪 I really enjoy it
SUBSCRIBE NOW
Dispatch is a must read for Android devs today and my go-to for keeping up with all things Jetpack Compose
SUBSCRIBE NOW
Dispatch has been my go-to resource as it's packed with useful information while being fun at the same time
SUBSCRIBE NOW
The content is light, fun, and still useful. I especially appreciate the small tips that are in each issue
SUBSCRIBE NOW
I truly love this newsletter ❤️🔥 Spot on content and I know there's a lot of effort that goes behind it
SUBSCRIBE NOW
Thanks for taking the time and energy to do it so well
JetpackCompose.app's Newsletter
I always learn something just by skimming it that makes me want to bookmark the issue now and dig deeper later
JetpackCompose.app's Newsletter
Keep up the good work with the newsletter 💪 I really enjoy it
JetpackCompose.app's Newsletter
Dispatch is a must read for Android devs today and my go-to for keeping up with all things Jetpack Compose
JetpackCompose.app's Newsletter
Dispatch has been my go-to resource as it's packed with useful information while being fun at the same time
JetpackCompose.app's Newsletter
The content is light, fun, and still useful. I especially appreciate the small tips that are in each issue
JetpackCompose.app's Newsletter
I truly love this newsletter ❤️🔥 Spot on content and I know there's a lot of effort that goes behind it
JetpackCompose.app's Newsletter
Thanks for taking the time and energy to do it so well
Equal size tiles
Author: Francesc Vilariño Güell
Custom layout to constraint and place children in slots of equal dimensions using a single measure pass
@Composable | |
fun EqualSizeTiles( | |
modifier: Modifier = Modifier, | |
content: @Composable () -> Unit, | |
) { | |
Layout( | |
content = content, | |
modifier = modifier, | |
) { measurables, constraints -> | |
layoutTiles( | |
measurables, | |
constraints | |
) | |
} | |
} | |
private fun MeasureScope.layoutTiles( | |
measurables: List<Measurable>, | |
constraints: Constraints, | |
): MeasureResult { | |
val tileHeight = constraints.maxHeight | |
val tileWidths = measurables.map { measurable -> | |
measurable.maxIntrinsicWidth(tileHeight) | |
} | |
val tileWidth = tileWidths.maxOrNull() ?: 0 | |
val tileConstraints = Constraints( | |
minWidth = tileWidth, | |
minHeight = 0, | |
maxWidth = tileWidth, | |
maxHeight = constraints.maxHeight, | |
) | |
val placeables = measurables.map { measurable -> | |
measurable.measure(tileConstraints) | |
} | |
val width = (placeables.size * tileWidth).coerceAtMost(constraints.maxWidth) | |
return layout(width = width, height = tileHeight) { | |
placeables.forEachIndexed { index, placeable -> | |
placeable.place(tileWidth * index, 0) | |
} | |
} | |
} | |
@Preview(showBackground = true) | |
@Composable | |
private fun EqualSizeTilesPreview() { | |
WeatherSampleTheme { | |
Surface(modifier = Modifier | |
.width(512.dp) | |
.background(color = Color.Yellow)) { | |
EqualSizeTiles( | |
modifier = Modifier | |
.height(64.dp) | |
.background(color = Color.Green) | |
.padding(all = 8.dp) | |
) { | |
Text( | |
text = "Left", | |
textAlign = TextAlign.Center, | |
modifier = Modifier | |
.background(color = Color.Red) | |
.padding(all = 8.dp) | |
.fillMaxHeight(), | |
) | |
Text( | |
text = "Center", | |
textAlign = TextAlign.Center, | |
modifier = Modifier | |
.background(color = Color.Yellow) | |
.padding(all = 8.dp) | |
.fillMaxHeight(), | |
) | |
Text( | |
text = "Right element", | |
textAlign = TextAlign.Center, | |
modifier = Modifier | |
.background(color = Color.Blue) | |
.padding(all = 8.dp) | |
.fillMaxHeight(), | |
) | |
} | |
} | |
} | |
} |
Have a project you'd like to submit? Fill this form, will ya!
If you like this snippet, you might also like:
Maker OS is an all-in-one productivity system for developers
I built Maker OS to track, manage & organize my life. Now you can do it too!