Jetpack Compose: Getting Started

Aug 1 2023 · Kotlin 1.8.10, Android 13, Android Studio Flamingo

Part 1: Make a Simple Interface

04. Leverage Modifiers

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Building a Simple Layout Next episode: 05. Use a Button

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Rendering items on the screen is only one part of building user interfaces. You also need to be able to customize the appearance and behavior of the items that you render on the screen. So far, we have discussed how to create and render items on the screen, we will dive deeper into how to customize the appearance and behavior of the items that you want to render on the screen.

Modifier Class

Let us add some modifications to our ProfileBar Composable.

@Composable
fun ProfileBar(){

    Row(modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
    ){

        Image(
            modifier = Modifier.size(50.dp),
            painter = painterResource(id = R.drawable.ic_profile),
            contentDescription = null,
        )

        Column(
            verticalArrangement = Arrangement.SpaceBetween,
        ){

            Text(
                text = "Hello, Kodeco",
                style = MaterialTheme.typography.bodyLarge,
            )

        }

        Image(
            modifier = Modifier.size(50.dp),
            painter = painterResource(id = R.drawable.ic_notifications),
            contentDescription = null,
        )

    }

}

Column(
    verticalArrangement = Arrangement.SpaceBetween,
    modifier = Modifier
        .width(200.dp)
        .height(100.dp)
        .padding(16.dp)
){

    Text(
        text = "Hello, Kodeco",
        style = MaterialTheme.typography.bodyLarge,
    )


}

Error: This image is missing a width attribute

Please provide one in the form of ![width=50%](./images/04-profile-bar-modifications.png)

Order of Modifiers

As you continue to chain Modifiers, you will continue noticing the behavior might look different from what you expect. This is because the order in which you chain the modifiers matters. The order in which you chain the modifiers determines the order in which the modifiers are applied to the Composable.

Box(modifier = Modifier
          .size(50.dp)
          .background(color = Color.Red)
          .clip(CircleShape))

Box(modifier = Modifier
          .size(50.dp)
          .clip(CircleShape)
          .background(color = Color.Red))

@Composable
fun HelloBox(){

    Box(modifier = Modifier
          .padding(10.dp)
          .size(200.dp)
          .border(width = 2.dp, color = Color.Blue)){

          Text(
              text = "Hello World",
              modifier = Modifier
                  .padding(10.dp)
                  .matchParentSize())

      }

}
@Composable
fun HelloBox(){

    Box(modifier = Modifier
          .padding(10.dp)
          .size(200.dp)
          .border(width = 2.dp, color = Color.Blue)
          .matchParentSize()){

          Text(
              text = "Hello World",
              modifier = Modifier.padding(10.dp))

      }

}