Parisa Baastani
Parisa Baastani

Reputation: 1911

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

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

Upvotes: 14

Views: 7220

Answers (2)

Aditya Sharma
Aditya Sharma

Reputation: 131

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

Upvotes: 0

Parisa Baastani
Parisa Baastani

Reputation: 1911

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.

Upvotes: 27

Related Questions