What's the best way to center one item in a Column...
# compose
d
What's the best way to center one item in a Column without setting verticalArrangment (because other items should be arranged to the top)?
f
You can use the Spacer composable
s
Is this a out centering in the rest of the available space, or to the screen itself? If you have enough items so that the column is filled completely what do you want to happen? If those items are enough that your item that you want to center is pushed off of the first shown things, what do you want to happen? The question I'd say requires a bit more info.
d
The column will not be filled completely. Preferably I would center the item to the available space in the column. I know I could just offset it down based on screen height, but I would like to avoid this solution if possible.
s
> The column will not be filled completely > How do you know that? If you have any content in a column, provided a small enough screen it may not always be able to show all items in their entirety. If you want to center it in the rest of the available space, just do:
Copy code
.weight(1f).wrapContentHeight(alignment...)
And it will fill the rest of the height and then align itself according to whatever alignment you decide to pass in there.
d
Yeah you're right. And thank you, the weight is what I was looking for. Do you know if it's possible to use the weight modifier in a LazyColumn. Because from first glance, I can only make it work in a Column.
s
I am not sure there to be honest.
a
You cannot use
weight
in a
LazyColum
directly, as the always-scrollable LazyColumn manages infinite space in the lazy direction (and its content is not Composable but
item
or
items
). But for your
item
-Composable-content in the LazyColumn, you can use weight, as long as it does not weight-measure with unlimited space in the infinite direction. So e.g. you can use
weight
in a
Column
that is in the
LazyColumn
, but then you have to set a fixed
height
to the column. Then you can use the
weight
-modifier for the Composables in the Column. It always comes down to the parent-Composable constraining the min-size and the max-size of the child-Composables (or in the context of LazyList, not constraining...).