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.

Material Design Snackbar — http://bit.ly/2faEXH1

Enter 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)
.message("Message")
.actionText("Action")
.showCallback(new MyShowCallback())
.actionDismissCallback(snackbar -> Timber.d("Dismissed"))
.timeoutDismissCallback(this::onSnackbarTimeout)
.build()
.show();

Many different improvements have been made compared to the plain Snackbar.make route.


Create from an activity

Rather than specifying the view in which to show the Snackbar, you have a few different options.

  1. An activity
  2. An activity with parent finder interface
  3. A view

The builder pulls a selection of different preferences from the snackbarBuilderStyle theme attribute.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="snackbarBuilderStyle">@style/SampleSnackbarStyle</item>
</style>
<style name="SampleSnackbarStyle" parent="SnackbarBuilder">
<item name="snackbarBuilder_parentViewId">@id/coordinator</item>
<item name="snackbarBuilder_actionTextColor">#454545</item>
<item name="snackbarBuilder_messageTextColor">#987654</item>
<item name="snackbarBuilder_duration">indefinite</item>
<item name="snackbarBuilder_backgroundColor">#999999</item>
</style>

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.


Styling

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.

Callbacks

The 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 onSnackbarSwiped or 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.

  • Shown showCallback(SnackbarShowCallback callback)
  • Dismissed dismissCallback(SnackbarDismissCallback callback)
  • Action pressed actionDismissCallback(SnackbarActionDismissCallback callback)
  • Swiped away swipeDismissCallback(SnackbarSwipeDismissCallback callback)
  • Timed out timeoutDismissCallback(SnackbarTimeoutDismissCallback callback)
  • Manual dismiss call manualDismissCallback(SnackbarManualDismissCallback callback)
  • Dismissed due to another Snackbar being shown consecutiveDismissCallback(SnackbarConsecutiveDismissCallback callback)

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.

new SnackbarBuilder(this)
.message("Message")
.actionText("Action")
.showCallback(presenter::snackbarShown)
.timeoutDismissCallback(presenter::snackbarTimedOut)
.build()
.show();

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.

compile 'com.github.andrewlord1990:snackbarbuilder:0.6.0'

I welcome any suggestions for improvements or feel free to send me a pull request on GitHub.

Enjoy!