Android snackbar

When Google announced Material Design on 2014, They have added many new components including android snackbar. It is a simple popup message just like toast but it can include actions. Just like toasts, we can customize the look of the snackbar.

Steps included

  1. Add gradle dependency

  2. Create simple snackbar

  3. Add click action

  4. Customize android snackbar

1. Adding material dependency

In the app level build.gradle located under the app folder, add following lines in between dependency tag.

2. Create simple snackbar

First we have to assign a id for parent layout in activity_main.xml. I have simply gave "parent" id to our top level constraint layout.

Fetch reference to the parent layout in a ConstraintLayout object.

Then we will call make() method of the snackbar object with required parameters like text and duration and parent layout. and finally call the show() method to actually show the snackbar.

3. Add click action

You can add click action by calling setAction() on the snackbar object.

4. Custom Android snackbar

Customize action button color simply by calling setActionTextColor() on snackbar object. Pass a color as parameter. Below, I have passed a constant as color you can use your own custom color by using getResources().getColor(R.color.colorAccent) inside setActionTextColor() method.

1MB
snackbar.mp4

Last updated