From the course: Android Compose with Kotlin

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Modify a composable’s layout with box constraints

Modify a composable’s layout with box constraints - Kotlin Tutorial

From the course: Android Compose with Kotlin

Modify a composable’s layout with box constraints

- [Instructor] On our sample app speaker screen, we display each item using the speaker item composable. It works, but we can do better. Instead of having the speaker details laid out horizontally, we could instead lay them out vertically depending on our available space. This vertical layout is easier to read and better uses the foldable's current open state. This is where using BoxWithConstraints makes sense. It's a special composable that gives you access to the parent composable's constraints. It exposes the min and max height and widths via a constraints object. With that information, you can make runtime decisions about the UI. I like to compare it to Russian nesting dolls. When you see the whole set, you start with the biggest and then place the next biggest in decreasing size. This way, you efficiently nest them all. Similarly, BoxWithConstraints provides values like maxWidth that can be used to display different composables, thus optimizing the given space. In this example…

Contents