Open In App

How to Create a Collapsing Toolbar in Android using Jetpack Compose?

Last Updated : 20 Jan, 2023
Like Article

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:

Collapsing Toolbar in Android


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.


package com.geeksforgeeks.jccollapsingtoolbar
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
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?) {
        setContent {
            // Creating a Simple Scaffold
            // Layout for the application
                // 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
                        ) {
                            // Setting the Image as the first
                            // item and making it collapsible
                            item {
                                    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
                            // Displaying the remaining 100 items
                            items(items) {
                                    text = it,


In the below-recorded video, you can see that the Toolbar collapses when scrolled down and holds up when scrolled back to the top.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads