Android Snackbar Example

In this tutorial you will learn about android snackbar example.

Snackbar is a new UI widget introduced in material design. It is an advance version or we can say replacement of Toast widget. Snackbar is used to display a short message at the bottom of screen. The message can be dismissed by swiping it.

Simple Snackbar

First Argument: It is the root layout for activity. Here we have used CoordinatorLayout because it gives some extra functionality to Snackbar like if we have used floating button then when Snackbar is displayed the floating button automatically goes up to provide space for Snackbar. By using CoordinatorLayout we can also dismiss the Snackbar by swiping.

Second Argument: It is the message that you want to display in Snackbar.

Third Argument: Time period for which Snackbar should be displayed.

Snackbar.LENGTH_SHORT: Display for short time period

Snackbar.LENGTH_LONG: Display for long time period

Snackbar.LENGTH_INDEFINITE: Displayed until you close it.

Snackbar with Action Button

You can also add an action button in Snackbar by using setAction() method. It can be done in following way.

1

2

3

4

5

6

Snackbar.make(coordinatorLayout,"Snackbar with Action",Snackbar.LENGTH_LONG).setAction("OK",newView.OnClickListener(){