SnackbarBuilder — Unleash your Snackbars
The Android Design Support library introduced the
Snackbar, making it easy to show messages to your users. However, it isn’t particularly customisable and requires you to specify the view you want to attach it to each time.
SnackbarBuilder, a builder pattern for Snackbars, which not only makes them easier to create, but provides a host of extra customisations. The easiest way to demonstrate it, is through an example builder call.
Snackbar snackbar = new SnackbarBuilder(this)
.actionDismissCallback(snackbar -> Timber.d("Dismissed"))
Many different improvements have been made compared to the plain
Create from an activity
Rather than specifying the view in which to show the Snackbar, you have a few different options.
- An activity
- An activity with parent finder interface
- A view
The builder pulls a selection of different preferences from the
snackbarBuilderStyle theme attribute.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="SampleSnackbarStyle" parent="SnackbarBuilder">
One of these is the
snackbarBuilder_parentViewId attribute, which contains the parent view ID to use. As long as you have a view with this ID in your activity, you will be able to show Snackbars simply by passing the activity to the SnackbarBuilder constructor.
Instead, you can provide a finder interface, allowing you to customise how the parent is found. The interface receives the activity instance and must return a view in which to show the Snackbar. It is a functional interface and so you could provide a lambda expression or a method reference if you wish.
You can also just provide the view to use to the constructor, just as you can with the standard Snackbar creator method.
One of the main annoyances with the
Snackbar is that it has a dark background, but still takes the default text color from your theme. This text colour is often dark too, making the messages hard to read and requiring you to retrieve the
TextView yourself to change the text color.
SnackbarBuilder matches the Material Design specification and defaults the text color to white. Of course you can change the text colour if you wish.
There are many styling options built into SnackbarBuilder.
- Change the message and action text colour theme defaults and on a per-Snackbar basis.
- Append chunks of text to the message, in different colours.
- Display an icon to the left of the message.
- Specify a default duration to use through the theme.
- Change the background colour.
Snackbar.Callback abstract class, requires you to check the dismiss event integer, to determine which action caused the dismiss to occur. Also, it isn’t a functional interface, forcing you to either use an anonymous inner class or to create a full class within your project.
SnackbarBuilder provides an abstract
SnackbarCallback class, with a different method for each of the dismiss types. For example
onSnackbarTimedOut. This works well if you do the same thing for many different Snackbars.
Instead you can specify a callback for each type of dismiss event through a set of different functional interfaces.
- Action pressed
- Swiped away
- Timed out
- Manual dismiss call
- Dismissed due to another Snackbar being shown
You have the option of specifying a lambda expression or method reference for these callbacks. This can make the SnackbarBuilder very flexible and the Snackbar creation code very easy to read.
SnackbarBuilder comes with many different customisations and is much more flexible and in my opinion produces more readable code. If you agree, then you can get it from JCenter for use in your Android projects.
I welcome any suggestions for improvements or feel free to send me a pull request on GitHub.
SnackbarBuilder - Builder pattern for support library Snackbars, that makes them easier to customise and usegithub.com