Collapsing Toolbar in Jetpack Compose | Codebase
Project description and initial code
You can go to any article of this guide by clicking on one of the links below:
Collapsing Toolbar in Jetpack Compose
Project description
The project CollapsingToolbarInCompose is a native Android application whose UI is completely implemented in Jetpack Compose. It displays a list of items with a collapsing toolbar on top that behaves differently depending on its scroll flag.
The application is titled “Costa Rica Wildlife” and shows a list of exotic animals that can be found across this country. All the sample images are stored inside the res/drawable
folder to avoid requests to external servers for simplicity.
Initially, the screen looks like the following image:
Codebase
⚠️ ATTENTION: In this guide, we won’t build any new composable except for the new screen that we are going to create using the composables already present in the project. Although I will describe them all, I’ll limit myself to show only the most relevant parts of their code.
🔗 You can find the initial code on my GitHub account:
Branches
master
: Contains the initial code that we need to start working on the collapsing toolbar implementation.column_version
: Contains the resulting code using aColumn
.lazycolumn_version
: Contains the resulting code using aLazyColumn
.
Package Organization
If you open the project and display its directory tree structure under the ‘Android’ view option, you will see the following:
There are two packages: data
and ui
. In the context of an MVVM or MVP architecture, these packages represent the Model layer and the View layer respectively.
Data package
Inside the data
package, you can find another package named model
. The model
package contains the only data class required for our purpose.
— Animal.kt
This is the only data class which contains the animal’s name and its drawable resource ID that points to its respective image.
UI package
The ui
package contains two packages: theme
and composables
.
theme
: Contains all the files that Android Studio adds when a new project is created. Those files are the ones where we define colors, shapes, typography and the theme of our application.
💬 I’ll omit the description of those files since they are present in every project and you should be familiar with them already.
composables
: Contains all the reusable composables that we need to build our UI with a collapsing toolbar functionality. These reusable composables are: the toolbar, both lists — one built with aColumn
and the other one with aLazyColumn
—, and aCard
.
Files and Composables
— AnimalCard.kt
The composable function AnimalCard
defines the composable that will be used for displaying each item of the list and uses a Card
as main structure.
Input Parameters:
animal
: Instance of the animal to be displayed. It holds its name and its drawable resource ID.modifier
: Optional parameter applied to theCard
.
This is what it looks like:
— Toolbar.kt
The composable function CollapsingToolbar
defines the toolbar and uses a Surface
as main structure.
Input Parameters:
backgroundImageResId
: Drawable resource ID to be displayed as background.progress
: Value between0f
and1f
, where1f
corresponds to its expanded state and0f
corresponds to its collapsed state.onPrivacyTipButtonClicked
: Lambda that defines the event to be triggered when the ‘Privacy Tip’ button is clicked.onSettingsButtonClicked
: Lambda that defines the event to be triggered when the ‘Settings’ button is clicked.modifier
: Optional parameter applied to theSurface
.
This is what it looks like in its collapsed state:
This is what it looks like in its expanded state:
💬 If you’re curious about the toolbar’s composition and dynamics, check out my article “A closer look at the Toolbar”, where I discuss it in more detail.
— EagerCatalog.kt
The composable function EagerCatalog
defines the scrollable component to be used along with the collapsing toolbar to form the screen that will be displayed. It uses a Column
as main structure. Unlike a LazyColumn
, a Column
doesn’t apply paddings to its content, so we need to reproduce that effect with Spacer
blocks.
Input Parameters:
animals
: List of animals used for populating theColumn
.columns
: Number of items that will be displayed per row.modifier
: Optional parameter applied to theColumn
.scrollState
: Optional parameter applied to theverticalScroll
modifier of theColumn
.contentPadding
: Optional parameter that determines the size of theSpacer
blocks that will be attached to the content of theColumn
.
— LazyCatalog.kt
The composable function LazyCatalog
defines the scrollable component to be used along with the collapsing toolbar to form the screen that will be displayed. It uses a LazyColumn
as main structure.
Input Parameters:
animals
: List of animals used for populating theLazyColumn
.columns
: Number of items that will be displayed per row.modifier
: Optional parameter applied to theLazyColumn
.listState
: Optional parameter applied to thestate
attribute of theLazyColumn
.contentPadding
: Optional parameter applied to thecontentPadding
attribute of theLazyColumn
.
This is what the entire list looks like in either version:
💬 You can use a
LazyVerticalGrid
instead of aLazyColumn
for the same grid effect.
— MainActivity.kt
This is the only Activity in the project, so this is the Activity that will be displayed when the application is launched. For simplicity, the list of animals is hardcoded and populated during onCreate
.
The composable function CollapsingToolbarInComposeApp
is called at the end of onCreate
and defines the screen to be displayed.
Input Parameters:
animals
: List of animals used for populating the content of the catalog.
And that’s it! If you’re curious about the composition and details of any of these composables, take a closer look at their code, explore and play around with them. In the next articles, we are going to build the new screen using all the composables presented in this article. I’ll be especially focusing on the different scroll flag implementations to make them interchangeable, so we will be able to change the toolbar behavior by modifying only one line of code.
Continue with the article of the version you’re interested in:
💬 If you enjoyed this article, you can show your appreciation by buying me a coffee at the link below. Thanks for reading and for your support.
Photo Credits
- 📷 Arenal Volcano by [Fabio Fistarol]
- 📷 Jaguar by [Omar Mena]
- 📷 Scarlet Macaw by [Shannon Kunkle]
- 📷 Baby Green Sea Turtle by [Charlotte Rush]
- 📷 Red-Eyed Tree Frog by [Zdeněk Macháček]
- 📷 Hoffman’s Two-Toed Sloth by [Adrián Valverde]
- 📷 Resplendent Quetzal by [Zdeněk Macháček]
- 📷 White-Nosed Coati by [Frank Williams]
- 📷 Green Iguana by [Alli Elder]
- 📷 Keel-Billed Toucan by [Zdeněk Macháček]
- 📷 Capuchin Monkey by [Etienne Delorieux]
- 📷 Emerald Basilisk by [Ray Harrington]
- 📷 King Vulture by [Birger Strahl]