A Jetpack Compose Collapsing Top Bar, that expands or collapses based on the scrolling of a content

Overview

CollapsingTopBarCompose

A Jetpack Compose Collapsing Top Bar, that expands or collapses based on the scrolling of a content

Centered expanded title and subtitle Centered expanded title without subtitle Left expanded title without subtitle
Demonstration 1 Demonstration 2 Demonstration 2

How to get this library in your android app

Step 1. Add the jitpack repository to the repositories { } function, inside your project build.gradle or your settings.gradle like so:

repositories {
    google()
    mavenCentral()
    // Place the jitpack repository inside this, like so:
    maven { url 'https://jitpack.io' }
}

Step 2. Add the dependency in your module build.gradle file, like so:

dependencies {
    implementation 'com.github.germainkevinbusiness:CollapsingTopBarCompose:1.0.0-alpha08'
}

Usage

Basic usage is shown below, there's a more elaborate example in the sample app .

In order to use a CollapsingTopBar, you first need to create a TopBarScrollBehavior.

 val scrollBehavior = remember {
    CollapsingTopBarDefaults.scrollBehavior(
        isAlwaysCollapsed = false,
        isExpandedWhenFirstDisplayed = true,
        collapsedTopBarHeight = 56.dp,
        expandedTopBarMaxHeight = 156.dp,
    )
}

To know when scrolling occurs inside your Layout so the CollapsingTopBar can collapse or expand, add the scrollBehavior.nestedScrollConnection inside your Layout's Modifier.nestedScroll :

Scaffold(
    modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
    topBar = {
        CollapsingTopBar(
            scrollBehavior = scrollBehavior,
            centeredTitleAndSubtitle = true, // set to false if you want the expanded title and subtitle to be at the left instead
            title = { Text(text = "All contacts") },
            subtitle = { Text(text = "17 contacts") },
        )
    },
) {}

So when we put it all together we got:

val scrollBehavior = remember {
    CollapsingTopBarDefaults.scrollBehavior(
        isAlwaysCollapsed = false,
        isExpandedWhenFirstDisplayed = true,
        collapsedTopBarHeight = 56.dp,
        expandedTopBarMaxHeight = 156.dp,
    )
}
Scaffold(
    modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
    topBar = {
        CollapsingTopBar(
            scrollBehavior = scrollBehavior,
            centeredTitleAndSubtitle = true, // set to false if you want the expanded title and subtitle to be at the left instead
            title = { Text(text = "All contacts") },
            subtitle = { Text(text = "17 contacts") },
        )
    },
) {
    LazyColumn(
        contentPadding = innerPadding,
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        val context = LocalContext.current
        val contactNames = context.resources.getStringArray(R.array.contactNames)
        items(count = contactNames.size) {
            ContactListNames(context, contactNames[it])
        }
    }
}

That's it!

License

Licenced under the MIT Licence

Copyright (c) 2022 Kevin Germain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

You might also like...
Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.
Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Compose.
This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Compose.

JetBMICalculator This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Co

Jetpack-Compose-Demo - Instagram Profile UI using Jetpack Compose
Jetpack-Compose-Demo - Instagram Profile UI using Jetpack Compose

Jetpack-Compose-Demo Instagram Profile UI using Jetpack Compose

Jetpack-compose-animations-examples - Cool animations implemented with Jetpack compose
Jetpack-compose-animations-examples - Cool animations implemented with Jetpack compose

Jetpack-compose-animations-examples This repository consists of 4 animations: St

Compose-navigation - Set of utils to help with integrating Jetpack Compose and Jetpack's Navigation

Jetpack Compose Navigation Set of utils to help with integrating Jetpack Compose

Jetpack-compose-uis - A collection of some UIs using Jetpack Compose. built using Katalog

Jetpack Compose UIs This is a collection of some UIs using Jetpack Compose. It i

A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose
A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose

Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree

😇Translation tool based on Jetpack Compose
😇Translation tool based on Jetpack Compose

📸 Screenshots   License Copyright 2020 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not u

📝   Note List app based on MVVM architecture (ViewModel, LiveData, Coroutines, Room and Jetpack Compose)
📝 Note List app based on MVVM architecture (ViewModel, LiveData, Coroutines, Room and Jetpack Compose)

Note list app built with Jetpack Compose Download Go to the releases page to download the latest available apk. Screenshots Architecture Features Home

Comments
Releases(1.1.6)
Owner
Germain Kevin
Android Software engineer | Android Library developer | Jetpack compose & XML | Programmer
Germain Kevin
Drawing text around other content in Jetpack Compose

TextAroundContent Drawing text around other content in Jetpack Compose Installat

Dmitry Mysenko 8 Feb 9, 2022
Capturable - 🚀Jetpack Compose utility library for capturing Composable content and transforming it into Bitmap Image🖼️

Capturable ?? A Jetpack Compose utility library for converting Composable content into Bitmap image ??️ . Made with ❤️ for Android Developers and Comp

Shreyas Patil 494 Dec 29, 2022
Kapture - A small library for Jetpack Compose to capture Composable content to Android Bitmap

kapture A small utility library for Jetpack Compose to capture Composable conten

Kaustubh Patange 10 Dec 9, 2022
Icontent - Jetpack Compose component to show all random content sended by Inmersoft guide backend

icontent IContent is a library that allows loading audio visual content generica

Orlando Novas Rodriguez 3 Nov 3, 2022
Gmail clone project, that uses Jetpack Compose to draw UI content for gmail home screen

Gmail clone project, that uses Jetpack Compose to draw UI content for gmail home screen following Udemy course: Android 12 Jetpack Compose Developer Course - From 0 To Hero

SaraAlshamy 3 Sep 2, 2022
Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Compose-Ratingbar-library - A simple implementation for rating bar in Jetpack Compose

Mahmoud Hussein 14 Dec 21, 2022
Create Bottom Navigation Bar with Jetpack Compose

BottomNavigationBarComposeExample Create Bottom Navigation Bar with Jetpack Compose https://johncodeos.com/how-to-create-bottom-navigation-bar-with-je

JohnCodeos.com 31 Dec 24, 2022
Jetpack Compose based project, used to stress-testing compose features / integrations and explore non-trivial functionality

Project containing Jetpack Compose samples For pagination & network images it uses CATAAS. Known issues Navigation-Compose Issue with fast tapping on

Denis Rudenko 59 Dec 14, 2022
Jetpack Compose Boids | Flocking Insect 🐜. bird or Fish simulation using Jetpack Compose Desktop 🚀, using Canvas API 🎨

?? ?? ?? Compose flocking Ants(boids) ?? ?? ?? Jetpack compose Boids | Flocking Insect. bird or Fish simulation using Jetpack Compose Desktop ?? , usi

Chetan Gupta 38 Sep 25, 2022
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Md. Mahmudul Hasan Shohag 186 Jan 1, 2023