Snackbar Android Tutorial with Floating Action Button

A new component introduced in Android Material Design is Snackbar.

It is almost same as Toast. But with some additional features. We can add an action in Snackbar. We can also customize colors of the text. So if we will talk about Snackbar vs Toast then snackbar is a winner. 😛

So in this Snackbar Android Tutorial we will see how to create and use Snackbar in Android Studio.

Snackbar Android Tutorial

  • For displaying a Snackbar we can use the following code.

  • The Snackbar.make() method takes three arguments
    1. View – Here we can use any View but it is recommended to use a CoordinatorLayout because it supports floating action button. If we use another view like LinearLayout then it will not support the floating action button.
    2. String – This is a string to display on the snackbar
    3. Length – The duration to display the snackbar, same as toast. We used LENGTH_LONG here
  • For adding an action to our Snackbar we can use the following code

  • We use setAction() method for adding an action to our snackbar it takes two parameters
    1. String – It will be shown as the action text
    2. Listener – An OnClickListener to perform action
  • Below you can see a snapshot of Snackbar 
snackbar android
Snackbar Android
  • For changing the color of Snackbar Message you can use setActionTextColor() method.
  • Changing the color for action text require the following code.

Applying Snackbar in Android Studio Project

  • Create a new Android Project (I created SnackbarExample) for our Android Snackbar Tutorial.
  • Goto build.gradle and inside dependencies add the following code and then sync your project with gradle.

  • Now come to activity_main.xml and create the following layout.
snackbar android
Snackbar Android
  • Use the following code for the above layout.

  • As you can see I am using CoordinatorLayout and a FloatingActionButton here.
  • Now come to and write the following code.

  • Now run your app.

Android Snackbar Example – Video Demo

  • You can also download the source code from below

Download Snackbar Android Example Source

So thats all for this Snackbar Android Tutorial friends. Feel free to ask if having any confusion regarding this Snackbar Android Tutorial. And share this Snackbar Android Tutorial among your friends, if you found it useful. Thank You 🙂

4 thoughts on “Snackbar Android Tutorial with Floating Action Button”

Leave a Comment