Android snackbar
Last updated
Last updated
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
Add gradle dependency
Create simple snackbar
Add click action
Customize android snackbar
In the app level build.gradle located under the app folder, add following lines in between dependency tag.
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.
You can add click action by calling setAction() on the snackbar object.
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.