An Android Jetpack Compose library for displaying on-screen messages

Overview

Maven Central Android Arsenal

InfoBar Compose

An Android Jetpack Compose library for displaying on-screen messages. Unlike the built-in Snackbar from the Compose Material library, the InfoBar can be properly displayed without additional requirements, like Scaffold, SnackbarHost / SnackbarHostState, or manually starting new coroutines to show the on-screen message.

Although the InfoBar composable is inspired by the Snackbar, it does not aim to entirely copy its design or behavior.

Test drive this library, demo app available on Google Play!

standard_info_bar_with_string_title_no_wrap_1080x181 standard_info_bar_with_string_title_and_action_1080x181

Usage

Include the library in your module-level build.gradle file:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.radusalagean:info-bar-compose:1.0.0'
}

The simplest configuration of an InfoBar is showcased below:

var message: InfoBarMessage? by remember { mutableStateOf(null) }

// Assign the message on an event callback (button click, download complete, message received, etc.):
// message = InfoBarMessage(text = "Example message")

InfoBar(offeredMessage = message) {
    // ⚠️ Important step: We are nulling out the message in the trailing lambda (onDismiss function)
    message = null
}

A complete working example:

setContent {
    YourAppTheme {
        Box(Modifier.fillMaxSize().padding(16.dp)) {
            var message: InfoBarMessage? by remember { mutableStateOf(null) }
            Button(
                modifier = Modifier.align(Alignment.Center),
                onClick = { message = InfoBarMessage(text = "Example message") }
            ) {
                Text("Show message")
            }
            InfoBar(offeredMessage = message) {
                message = null
            }
        }
    }
}

simple-usage

InfoBar types

The InfoBar composable has 2 signatures:

  • One for a standard version, which already has a layout defined, inspired by the Material design Snackbar. This composable has multiple parameters exposed, allowing for increased customizability of the predefined standard layout.
  • One for a generic version, allowing to pass a composable layout defined in the client app.

Configuration parameters

InfoBar composable:

S G
Available in the Standard InfoBar Available in the Generic InfoBar
Parameter Description Type S G
modifier Modifier to be applied to the InfoBar surface Modifier ✔️ ✔️
offeredMessage InfoBarMessage or BaseInfoBarMessage subclass instance, describing the message that should be displayed InfoBarMessage? ✔️ ✔️
elevation Elevation to be applied to the InfoBar surface Dp ✔️ ✔️
shape Shape to be applied to the InfoBar surface Shape ✔️ ✔️
backgroundColor Background color to be applied to the InfoBar surface Color? ✔️ ✔️
content The content composable to use in the InfoBar surface @Composable (T) -> Unit ✔️
textVerticalPadding Vertical padding for the message text Dp ✔️
textColor Color for the message text Color? ✔️
textFontSize Font size for the message text TextUnit ✔️
textFontStyle Font style for the message text FontStyle? ✔️
textFontWeight Font weight for the message text FontWeight? ✔️
textFontFamily Font family for the message text FontFamily? ✔️
textLetterSpacing Letter spacing for the message text TextUnit ✔️
textDecoration Decoration for the message text TextDecoration? ✔️
textAlign Alignment for the message text TextAlign? ✔️
textLineHeight Line height for the message text TextUnit ✔️
textMaxLines Maximum number of lines for the message text Int ✔️
textStyle Style for the message text TextStyle ✔️
actionColor Color for the action button text Color? ✔️
fadeEffect Use fading effect when the message appears and disappears? (controls the alpha property) Boolean ✔️ ✔️
fadeEffectEasing Easing style of the fade effect InfoBarEasing ✔️ ✔️
scaleEffect Use scaling effect when the message appears and disappears? (controls the scaleX / scaleY properties) Boolean ✔️ ✔️
scaleEffectEasing Easing style of the scale effect InfoBarEasing ✔️ ✔️
slideEffect Which sliding effect to use when the message appears and disappears? (controls the translationY property) InfoBarSlideEffect ✔️ ✔️
slideEffectEasing Easing style of the slide effect InfoBarEasing ✔️ ✔️
enterTransitionMillis Enter animation duration in milliseconds Int ✔️ ✔️
exitTransitionMillis Exit animation duration in milliseconds Int ✔️ ✔️
wrapInsideExpandedBox Maintain the shadow of the InfoBar even when animating the alpha property, by wrapping the InfoBar content inside a Box layout that fills the maximum available space. The alpha property is then animated on the outer Box instead of the InfoBar surface, thus not clipping the shadow when alpha is less than 1f. Note: Any modifier you pass from the outside is applied to the InfoBar surface, not the outer Box layout! Boolean ✔️ ✔️
onDismiss Function which is called when the InfoBar is either timed out or dismissed by the user. Don't forget to always null out the InfoBarMessage / BaseInfoBarMessage subclass instance here! (see usage example from above) () -> Unit ✔️ ✔️

InfoBarMessage class:

Parameter Description Type
text Message as string String?
textStringResId Message as string resource id Int?
textStringResArgs Arguments for textStringResId Array ?
textColor Color for the message text (overrides textColor set in the InfoBar composable) Color?
action Action as string String?
actionStringResId Action as string resource id Int?
actionStringResArgs Arguments for actionStringResId Array ?
actionColor Color for the action button text (overrides actionColor set in the InfoBar composable) Color?
backgroundColor Background color to be applied to the InfoBar surface (overrides backgroundColor set in the InfoBar composable) Color?
displayTimeSeconds The number of seconds to display the message (excluding animation time). Pass -1 if you don't want the message to time out. Int?
onAction Function which is called when the user presses the action button (() -> Unit)?

Using the generic composable

If the standard InfoBar signature does not entirely meet your requirements in terms of layout, you can use the generic InfoBar signature, which allows you to pass a custom layout composable:

  1. Extend the BaseInfoBarMessage abstract class and define your custom message data structure:

    class CustomMessage(
        val textString: String,
        val icon: ImageVector,
        val iconColor: Color,
        val textColor: Color = Color.Unspecified,
        override val backgroundColor: Color? = null,
        override val displayTimeSeconds: Int? = 4,
    ) : BaseInfoBarMessage() {
        override val containsControls: Boolean = false
    }
  2. Declare the content composable that defines your layout:

    val content: @Composable (CustomMessage) -> Unit = { message ->
        Row {
            Icon(
                modifier = Modifier.padding(8.dp).align(Alignment.CenterVertically),
                imageVector = message.icon,
                contentDescription = null,
                tint = message.iconColor
            )
            Text(
                modifier = Modifier.align(Alignment.CenterVertically),
                text = message.textString,
                color = message.textColor
            )
        }
    }
  3. Display the message:

    setContent {
        YourAppTheme {
            Box(Modifier.fillMaxSize().padding(16.dp)) {
                var message: CustomMessage? by remember { mutableStateOf(null) }
                Button(
                    modifier = Modifier.align(Alignment.Center),
                    onClick = { 
                        message = CustomMessage(
                            textString = "This is a custom message",
                            textColor = Color(0xFF414141),
                            icon = Icons.Rounded.Info,
                            iconColor = Color(0xFF27C54D),
                            backgroundColor = Color(0xFFE3F1E6)
                        )
                    }
                ) {
                    Text("Show message")
                }
                InfoBar(offeredMessage = message, content = content) {
                    message = null
                }
            }
        }
    }

    custom-usage

Sample App

More complex usage examples are available in the sample app. Download from Google Play.

sample-app

Photo credits: The sample app contains 2 photos, used under Pexels license. The authors of these photos are Kittichai Chumanee and Karolina Grabowska.

Contributions

Found a bug or have a suggestion? Please open an issue.

Support

If you use this library and enjoy it, please support it by starring it on GitHub. 🌟

Further reading

There is an article available that showcases this library in a bit more detail.

License

Apache License 2.0, see the LICENSE file for details.

You might also like...
 Yet another awesome Intro screen library 🔥
Yet another awesome Intro screen library 🔥

Compose-Intro ✅ Yes, I'm too bad in naming my libraries : ) Yet another Intro Screen library made with the love of Kotlin and Jetpack compose : ) ❤️ ✨

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.

Jetpack Compose Boids | Flocking Insect 🐜. bird or Fish simulation using Jetpack Compose Desktop 🚀, using Canvas API 🎨
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

A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!
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!

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

Releases(v1.0.0)
Owner
Radu Salagean
I focus mostly on Android Development, including open source apps and libraries.
Radu Salagean
Dynamic Badge with customizable features as max number before displaying with +, color, shadow, border, corner radius, font properties and more written with Jetpack Compose

✏️?? Dynamic Badge with customizable features as max number before displaying with +, color, shadow, border, corner radius, font properties and more written with Jetpack Compose. Displays numbers either in circle or rounded rectangle shape based on badge count and selected threshold to transform from circle to rounded rectangle.

Smart Tool Factory 4 Jul 27, 2022
🚀🏞💪 Collection of Images, Modifiers, utility functions for Jetpack Compose to expand and enrich displaying, manipulating, scaling, resizing, zooming, and getting cropped ImageBitmap based on selection area

Collection of Images, Modifiers, utility functions for Jetpack Compose to expand and enrich displaying, manipulating, scaling, resizing, zooming, and getting cropped ImageBitmap based on selection area, before/after image to with handle to show partial of both images and more is cooking up

Smart Tool Factory 207 Dec 26, 2022
A Jetpack Compose component used for displaying Markdown-formatted text.

MarkdownText A library for displaying Markdown contents within Jetpack Compose. Uses Coil Current limitations Lists that are annotated with the * must

Arnau Mora 4 Dec 15, 2022
Compose-Instagram-Profile-UI - Instagram profile screen UI using android jetpack compose

Compose-Intsgram-Profile-UI Instagram profile screen UI using android jetpack co

TILLERN 1 Mar 8, 2022
Instagram Profile Screen UI With Jetpack Compose

Instagram Profile Screen With Jetpack Compose In this repository, i made instagram profile screen. And i used jetpack compose. While i make this app,

Adem ATİCİ 10 Mar 6, 2022
ComposeLoginScreen - A sleek design of a simple login screen using Jetpack Compose

ComposeLoginScreen A sleek design of a simple login screen using Jetpack Compose

ComposeDesign 19 Dec 5, 2022
Taxi - an example of how to create an introductory screen using Jetpack Compose

Taxi - an example of how to create an introductory screen using Jetpack Compose

Dmitry Savin 1 Feb 25, 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
Short example displaying an editable list

MVVM Profiles list App displaying a list of profiles with the ability to add, remove and edit each one Project characteristics 100% Kotlin MVVM Flow A

Gonzalo Guzzardi 0 Jan 15, 2022
GameDetailScreenCompose - Game Detail Screen Compose With Kotlin

GameDetailScreenCompose Домашнее задание от Podlodka Crew. "Нужно сверстать экра

slartus 2 Feb 19, 2022