Your Second Kotlin Android App

Aug 29 2023 · Kotlin 1.8.21, Android 13, Android Studio Flamingo | 2022.2.1

Part 2: Create Your Views

10. Learn Compose Layouts

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: 09. Introduction Next episode: 11. Build a List Item View

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.

Transcript: 10. Learn Compose Layouts

In this episode, you’ll explore how to create beautiful, responsive, and efficient user interfaces using Jetpack Compose. You’ll understand the basics of Compose layout and how to work with Compose layouts.

Designing UIs in Compose is much easier. The state is transformed into UI by the following steps:

  • Composition - This is the first phase where Compose creates a tree of UI elements, with each element being a function that returns a UI element. Compose then calls the functions to create the UI tree. The composition phase is responsible for determining which Composables need to be updated and which ones can be reused from the previous frame. This is done by comparing the previous tree of Composables with the new tree and only updating the ones that have changed, thereby making the process efficient.

  • Layout - In this phase, Compose takes the tree of Composables created in the composition phase and determines their position, size, and layout. This is done by measuring the size of each Composable and positioning it within the layout based on its parent and any constraints that have been set. It is also responsible for creating the final layout tree that will be used in the drawing phase.

  • Drawing - In this phase, Compose takes the final layout tree created in the layout phase and uses it to draw the UI elements on the screen. This is done by traversing the tree and issuing drawing commands to the underlying graphics system. The drawing phase also takes care of any animations or transitions that have been defined.

Overall, the composition, layout, and drawing phases work together to create and render the UI in Compose. This process is highly optimized, performant, and efficient, allowing for fast and smooth UI rendering even in complex apps.

Compose provides basic layouts named Row and Column to organize UI elements horizontally and vertically, respectively. Later in this course, you’ll learn how to add child elements to these layouts.

Compose also provides the Box component, which allows you to position child elements flexibly using x and y coordinates.

There’s also the Spacer component that allows you to add space between child elements.

Another layout available is the ConstraintLayout, which enables you to create complex and responsive layouts by using relative positioning.

Material Design already provides a wide range of components for you to use in your app, some of them being:

  • Text Fields
  • Buttons
  • Cards
  • Dialogs
  • Switches, etc.

Material Design also has a layout component named Scaffold that provides a basic structure for your app. It offers slots for various components like the top app bar, snack bars, bottom navigation bar, and a floating action button.

Most apps require displaying a list of items to the user. Compose provides the LazyColumn and LazyRow components that you can use to display a list of items in a highly efficient and performant manner. They only render the items that are visible on the screen, rather than rendering all the items at once. LazyColumn displays items vertically, whereas LazyRow displays items horizontally. These composable functions allow you to define the contents of the list as a function that returns a single item, and then Compose will automatically generate and render the UI elements for each item in the list as they become visible on the screen.

The LazyColumn and LazyRow composable functions both take a parameter named items, which is a list of data objects to be displayed in the list. They also take an item parameter, which is a function that takes an item from the list and returns a Composable that represents the UI element for that item.

There are also LazyVerticalGrid and LazyHorizontalGrid components that display items vertically and horizontally respectively in a grid layout.