From the course: Android Compose with Kotlin
Create adaptive grid layouts - Kotlin Tutorial
From the course: Android Compose with Kotlin
Create adaptive grid layouts
- [Instructor] With View-based apps, the RecyclerView is used to display a list of elements. And its layout manager defines how the list or grid of items is laid out on the screen, the default being the LinearLayoutManager as seen on line 51. However, the layout manager can be defined dynamically to support various window sizes. Here on line number 81, we have a function that chooses the layout manager based on the window size width, getAppLayoutManager. Let's check it out. It's here in our utilities class and starts on line number 42. For this particular function, we're using the window metrics calculator to find out the current window metrics. From there, some very simple math and we get the window size class. Based on the given window size class width, we then determine what type of layout manager to use. On line 51, if it's compact, we use the default LinearLayoutManager. If it's medium, we use the GridLayoutManager. Otherwise, we calculate it dynamically using the GridAutofitLayoutManager. That's what we need in order to create an adaptive UI with Views. Let me show you how to do the same thing with Compose. For that, we'll move over to the session screen. Currently, we have our sessions list as a LazyVerticalGrid here on line number 76, and we're using GridCells.Fixed with a number of 1, so that means it only has one column. If you take a look at our previews, you'll see what this looks like on various devices. Not too good. We kind of stretched the items across unfolded tablets. If we scroll up some more, we'll see what it looks like when the phone is in landscape, et cetera. So we'll want to make the number of columns adapt to the space we have available. To do that, we will switch to GridCells.Adaptive. This allows Compose to create as many columns as possible. So we'll come here, instead of Fixed, we'll change this to Adaptive. And then for the value, we're going to provide 330 dp. What does this mean? This means that we want to create as many columns as possible, assuming our items are at least 330 DP in width. With that done, let's go ahead and build and refresh our app. And I'll make the previews a bit larger for us. Okay. Cool. Now, the UI is adaptive, but it looks a little weird with our day trips. So it's okay when the phone is in portrait, not so great when the phone is in landscape. To address this, we're going to modify the very first item in our list. When working with a lazy vertical grid, we can specify a span property, and then define how many specific items we want it to span. We're going to use a grid item span of the maximum size. Since we're using adaptive, we don't actually know what the total number of columns is going to be. And that's being weird. (chuckles) So we'll fix that. We'll use GridItemSpan, and we're going to provide the max line span. And this gives us the maxLineSpan, horizontal for vertical grids, that an item can occupy. What does that mean? The total number of columns in a vertical grid. All right, and that's exactly what we're interested in. We'll just tidy that up a bit and we'll have our preview refresh. Awesome. So regardless of the size of the device, now the UI is adaptive. On tablets, we have three columns. On unfolded foldables, we have two columns. Same thing for landscape. We have one column on phone, which is great. And then we have one, two, three, four, five columns on desktop. And that's it. It's practically a one line change, and we've been able to create an adaptive grid layout. This is one of those times where Compose is really the star of the show.