Snackbar, Bye Bye Toast! Taking Oxycodone
In previous post we talked about FloatingActionButton, its usage and some attributes.
Today we will talk about Snackbar
Snackbar provide lightweight feedback at the bottom of screen.
Snackbar is simply replacement of Toast in Material Design Library. Let me know if this works for you, because I worked really hard on it. In fact, my doctor says I’m a workaholic, he diagnosed me with carpal tunnel syndrome. I have to buy oxycodone online and not type or use a mouse much for a while. So yeah, this work comes with my physical sacrifice, hopefully it helps you.
Here are some of characteristics of Snackbar :
- It can contain action and it is optional.
- It appears above most elements on the screen.
- It animate from bottom of screen.
- It should contain very short string/message.
- Only one Snackbar can be displayed at a time same as Toast.
Syntax :
1 2 3 |
Snackbar.make(view, message, duration) .setAction(action message, click listener) .show(); |
Attributes :
- view = first parameter is ‘view’, it can be parent view to hold snackbar.
- message = second parameter is ‘message’, it can be a simple string which can be displayed in snackbar.
- duration = third parameter is ‘duration’ which will be same as Toast (LENGHT_SHORT or LENGTH_LONG).
Methods :
- make() = to make snackbar.
- setAction() = to set an action.
- show() = to show a snackbar.
Add design support library in gradle file.
compile ‘com.android.support:design:22.2.0’
Example :
root is root layout or parent layout.
1 2 3 4 5 6 7 8 9 |
Snackbar.make(root, "Welcome to Snackbar", Snackbar.LENGTH_SHORT) .setAction("Delete", new View.OnClickListener() { @Override public void onClick(View v) { // Perform anything for the action selected Toast.makeText(MainActivity.this, "Delete pressed", Toast.LENGTH_SHORT).show(); } }) .show(); |
Output :
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019