Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the drawing order of a layout children in Android Jetpack Compose?

By default in Jetpack Compose a layout children rendering order matches the order of the children in the code. In the following example the ship (Text) will be drawn over the water (Box).

@Composable
fun DrawingOrderExample(submarineMode: Boolean) {
    Box(contentAlignment = Alignment.Center) {
        Box(
            modifier = Modifier
                .size(32.dp)
                .background(Color.Cyan.copy(alpha = .5f))
        )

        Text("⛵")
    }
}

Can I force the ship to be drawn under the water according to submarineMode argument?

like image 575
Valeriy Katkov Avatar asked Jan 27 '21 16:01

Valeriy Katkov


People also ask

What are layouts in jetpack compose?

Layouts are the building blocks to design the UI. In the traditional Android development, we have RelativeLayout, LinearLayout, FrameLayout, ConstraintLayout, etc. Likewise, Jetpack Compose offers four types of layouts at the moment:

What do you need to use jetpack compose for Android?

You will need to install Android Studio Canary, Arctic Fox (2020.3.1) version to use the Jetpack Compose beta. Layouts are the building blocks to design the UI. In the traditional Android development, we have RelativeLayout, LinearLayout, FrameLayout, ConstraintLayout, etc. Likewise, Jetpack Compose offers four types of layouts at the moment:

How do I set children's position within a row in compose?

Note: Compose handles nested layouts efficiently, making them a great way to design a complicated UI. This is an improvement from Android Views, where you need to avoid nested layouts for performance reasons. To set children's position within a Row, set the horizontalArrangement and verticalAlignment arguments.

What are jetpack compose modifiers?

The modifier design makes this kind of behavior explicit and predictable, and gives you more control to achieve the exact behavior you want. It also explains why there is not a margin modifier but only a padding one. Jetpack Compose provides a list of built-in modifiers to help you decorate or augment a composable.


1 Answers

You can use zIndex() modifier to change the children drawing order:

...

import androidx.compose.ui.zIndex

Box {
    Text("Drawn second", Modifier.zIndex(1f))
    Text("Drawn first")
}

The ship example:

@Composable
fun DrawingOrderExample(submarineMode: Boolean) {
    Box(contentAlignment = Alignment.Center) {
        Box(
            modifier = Modifier
                .size(32.dp)
                .background(Color.Cyan.copy(alpha = .5f))
        )

        val shipZIndex = if (submarineMode) -1f else 1f

        Text(
            text = "⛵",
            modifier = Modifier.zIndex(shipZIndex) // <- here's the trick
        )
    }
}

Now submarineMode argument affects the drawing order:

enter image description here enter image description here

like image 156
Valeriy Katkov Avatar answered Oct 27 '22 12:10

Valeriy Katkov