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. …

snackbar android

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 MainActivity.java 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

[sociallocker id=”1372″] [download id=”1345″] [/sociallocker]

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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x