Thomas Hormes
07/07/2023, 7:33 AMscrollable
Modifier, the constraints.maxWidth
is automatically set to Infinity, which makes sense as the Composable is infinitely scrollable.
However, for the layout I'm currently building I also need to get the width of the Layout that is visible.
So say for example that if my Layout is vertically Scrollable and used in a Column that spans the whole screen and has a horizontal padding of 16Dp, that would make the visible area screenSize - 2*16Dp
wide.
Is there any way to get the size of this visible area using a Layout
or SubcomposeLayout
?Thomas Hormes
07/07/2023, 8:07 AMBoxWithConstraints
and then settings the maxWidth
of the BoxWithConstraints
as the minWidth
of my Layout, then resetting the constraints to have a minWidth
of 0, as to not mess up measurement of the children of my layout like so:
BoxWithConstraints {
SubcomposeLayout(
modifier = modifier.defaultMinSize(this.maxWidth)
) { constraints ->
val visibleArea = constraints.minWidth
val constraints = constraints.copy(minWidth = 0)
}
}
However, this feels like a super hacky solution. Are there any better ways?Stylianos Gakis
07/07/2023, 8:41 AMLazyColumn
does by having the contentPadding
parameter?Thomas Hormes
07/07/2023, 9:48 AMStylianos Gakis
07/07/2023, 9:49 AMStylianos Gakis
07/07/2023, 9:50 AMThomas Hormes
07/07/2023, 10:06 AMn
elements, where n is unknown.
If the Elements do not completly fill the Width the Layout takes up, I want to evenly space them across the Width of my Layout.
If all the Elements are larger than the width the Layout takes up, I want them to be spaced by a predefined value and have the row be scrollable.
Kind of like in the attached sketch.
(This sketch does not capture the full complexity of the requirements of the layout, but do get the point across. A normal Row would unfortunately not do the job)Thomas Hormes
07/07/2023, 10:06 AMAnd with that said, one thing I’ve started doing recently was not provide screen-wide paddings, but only apply them to the children as needed.I have gotten into this habit myself due to the same reasons you stated 🙂 Was just trying to give a simple (even if flawed) example 😅
Stylianos Gakis
07/07/2023, 10:23 AMRow {
Spacer(Modifier.fillMaxHeight().width(20.dp).height(50.dp).debugBorder())
val intRange = 0..7
Row(Modifier.weight(1f).horizontalScroll(rememberScrollState())) {
Spacer(Modifier.width(8.dp))
Spacer(Modifier.weight(1f))
for (i in intRange) {
Item()
if (i != intRange.last) {
Spacer(Modifier.weight(1f))
Spacer(Modifier.width(8.dp))
}
}
Spacer(Modifier.weight(1f))
Spacer(Modifier.width(8.dp))
}
Spacer(Modifier.fillMaxHeight().width(20.dp).height(50.dp).debugBorder())
}
@Composable
private fun Item(modifier: Modifier = Modifier) {
Box(modifier.size(50.dp).background(Color.Red, CircleShape))
}
Stylianos Gakis
07/07/2023, 10:23 AMval intRange = 0..X
Stylianos Gakis
07/07/2023, 10:26 AMI want them to be spaced by a predefined value and have the row be scrollable.
Thomas Hormes
07/07/2023, 11:51 AMBoxWithConstraints
hack.
I guess I just want to know whether there is a less hacky way to do thisStylianos Gakis
07/07/2023, 12:04 PMZach Klippenstein (he/him) [MOD]
07/07/2023, 7:51 PMonSizeChanged
modifier right before your scrollable
modifier and save the size from there. BoxWithConstraints
is definitely not required.Stylianos Gakis
08/29/2023, 2:12 PMonSizeChanged
right before the scroll modifier was the solution, ah so glad this exists I was not finding any way to fix this. Thanks a lot Zach 🙏