In previous posts we have seen toast to display a message, alert box or dialog box to display a message the message may be output or may be sometimes system error depending upon requirement but now we have similar display system called as Snackbar.

Snackbar displays a piece of message at the bottom of the screen which appears much more stylish than the normal toast so we will see it in the present tutorial.

This Snackbar is made available with latest api supporting design.This is a material design  available for providing high end design.   snackbar3

Snackbar will look as

snackbar4

 

 

If you notice here you will find a display message as AndroidCoding.in and there is also a action button which you can notice it is as “OK” i.e., when you click on OK you will get a Toast i have made a Toast to be appeared here but can code anything in place of that.

 

Add support:design which is required for CoordinatorLayout whic is used to display a Snackbar in android add it to your gradle file as

 

Creating snackbar.xml :

 

Adding a CoordinatorLayout to the activity file and also a button. This CoordinatorLayout  helps in displaying a message and also a action button which will perform a desired action on user click.

CoordinatorLayout :

 

Creating SnackBarActivity.java :

Firstly initialize snackbarCoordinatorLayout

And now initialize button and also set on click listener

  As you can notice we have also given time duration as Long for the snack bar from below line. 

And now we can set the Action for the Snackbar this is the main feature as i consider because in toast there is no button for user to accept it or perform any action but in Snackbar it is accepting a action by which you can get a piece of work done a s you can move from activity to another or sny thing which you want it be done when you click on that action button and the coding for it goes on like this

At last don’t forget to add this line as this plays a crucial role in showing a Snackbar on user screen i.e., show functionality and its not anything new you have seen it for Toast, AlertBox and  a few more….

 

Output :

snackbar1 snackbar2 snackbar3

Show Buttons
Hide Buttons
Read previous post:
Android Tutorial on Vibration | How to vibrate device on button click

Close