Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Jetpack Compose spacer with weight and min height

I have a Column that has a modifier with verticalScroll() on it that fills the screen height. There are elements at the top and bottom of the column that have a third element centred between them using two spacers with weights of 1.0.

Occasionally elements may be added that make the column longer than the screen and activate the scroll. At that point the spacers shrink to nothing since they no longer need to fill space to fit the screen.

I've tried adding a height attribute in conjunction with the weight but that seems to have no effect. How do I have spacers fill space while also constraining to a height minimum?

Code:

Column(
    modifier = Modifier
    .fillMaxWidth()
    .verticalScroll(rememberScrollState())
    .fillMaxHeight(),
    verticalArrangement = Arrangement.Center,
    horizontalAlignment = Alignment.CenterHorizontally
) {
    PotentialOptionalElements()
    PageHeader()
    Spacer(Modifier.weight(1.0f))
    PageContent()
    Spacer(Modifier.weight(1.0f))
    PageFooter()
}
    
like image 204
Marsroverr Avatar asked Oct 16 '25 16:10

Marsroverr


2 Answers

Barring other options - The following works:

Spacer(Modifier.weight(1.0f))
Spacer(Modifier.height(16.dp))

Would however still prefer a solution that uses a single spacer

like image 156
Marsroverr Avatar answered Oct 18 '25 08:10

Marsroverr


I found out that this combination of modifiers works:

Spacer(modifier = Modifier
            .weight(1f)
            .requiredHeight(16.dp))
like image 38
Olesya Avatar answered Oct 18 '25 06:10

Olesya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!