Jetpack Compose: Getting Started

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

Part 2: Layout Your App

08. Add Spacers

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: 07. Columns & Rows Next episode: 09. Organize Your Code

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

You have been able to use the Column and Row Composables to arrange Composables vertically and horizontally respectively.

Spacer

Let us add some space between our Image and Text Composables in the FoodCategoryItem component.


Column(
      modifier = Modifier.size(75.dp),
      horizontalAlignment = Alignment.CenterHorizontally,
  ) {

    Image(
        modifier = Modifier.size(75.dp),
        painter = painterResource(id = icon),
        contentDescription = null
    )

    // Add a 5.dp spacer between the image and the text
    Spacer(modifier = Modifier.size(5.dp))

    // remove the top padding from the text
    Text(
        modifier = Modifier,
        text = category,
        textAlign = TextAlign.Center,
        fontWeight = FontWeight.Bold,
        style = MaterialTheme.typography.bodyMedium
    )

  }

Weight

The weight Modifier function is used to specify how much space an item should take up within a Column or Row Composable.

Weight

Row(modifier = Modifier
      .fillMaxSize()
      .padding(16.dp),
      verticalAlignment = Alignment.Top) {

    Text(text = "Hello World", modifier = Modifier.weight(2f))

    Spacer(modifier = Modifier.weight(1f))

    Text(text = "Hello Columns", modifier = Modifier
        .weight(1f))

    Text(text = "Hello Columns", modifier = Modifier
        .weight(1f))
  }

Conclusion

In this session, you learned how to use the Spacer Composable to add space between Composables. You also learned how to use the weight Modifier function to specify how much space an item should take up within a Column or Row Composable.