Jetpack Compose - How can add multiple modifier to a composable and is the order important?
Asked Answered
R

2

14

I wanted to know how we can add multiple modifier, for example adding background, padding and more to an android jetpack composable?

Remit answered 28/2, 2021 at 8:30 Comment(0)
R
27

It's really simple; You can chain multiple modifiers.

Column(modifier = Modifier.preferredHeight(500.dp).padding(100.dp)) {
Text("Hello")  }

And The order is important; Modifier elements to the left are applied before modifier elements to the right.

Remit answered 28/2, 2021 at 8:30 Comment(2)
How can I chain fillMaxSize with padding?Fredella
@Fredella Column(modifier = Modifier.padding(24.dp).fillMaxWidth()) { Text(text = "hello") Text(text = name) }Gubernatorial
E
0

if you have to use two different modifier, you can simply use .then() function to add second modifier

For Example, let's say you have a composable function in which we have passed a modifier as a parameter and inside that function we have a box which already have a modifier. so by using then modifier you can add the modifier which have been passed as a parameter

    @Composable
    fun myView(modifier: Modifier) {
        Box(
            modifier = Modifier
                .padding(8.dp)
                .background(Color.Black)
                .then(modifier)
        )

}

and yes order your in which you are adding modifiers matters

Elodea answered 28/8, 2023 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.