How to Create a Collapsing Toolbar in Android using Jetpack Compose?
A Toolbar in Android is an element present on the top of the application screen which generally displays the application name. In our example, we are creating a modified Toolbar i.e., Collapsing Toolbar that collapses when the application is scrolled down and holds up when scrolled up to the top. Below is the animation of how Collapsing Toolbar appears:
In this article, we will show you how you could create a Collapsing Toolbar in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.w3wiki.jccollapsingtoolbar import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { // Creating a Simple Scaffold // Layout for the application Scaffold( // Creating a Top Bar topBar = { TopAppBar(title = { Text( "GFG | Collapsing Toolbar" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) }, // Creating Content content = { // Creating a Column Layout Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { // Creating a Scrollable list of 100 items val items = ( 1 .. 100 ).map { "Item $it" } val lazyListState = rememberLazyListState() var scrolledY = 0f var previousOffset = 0 LazyColumn( Modifier.fillMaxSize(), lazyListState, ) { // Setting the Image as the first // item and making it collapsible item { Image( painter = painterResource(id = R.drawable.sample_image), contentDescription = null , contentScale = ContentScale.FillWidth, modifier = Modifier .graphicsLayer { scrolledY += lazyListState.firstVisibleItemScrollOffset - previousOffset translationY = scrolledY * 0 .5f previousOffset = lazyListState.firstVisibleItemScrollOffset } .height( 240 .dp) .fillMaxWidth() ) } // Displaying the remaining 100 items items(items) { Text( text = it, Modifier .background(Color.White) .fillMaxWidth() .padding( 8 .dp) ) } } } } ) } } } |
Output:
In the below-recorded video, you can see that the Toolbar collapses when scrolled down and holds up when scrolled back to the top.