Build and display a pop-up message

You can use a Snackbar to display a brief
message to the user. The message automatically goes away after a short
period. A Snackbar is ideal
for brief messages that the user doesn't necessarily need to act on. For
example, an email app could use a Snackbar to tell the user that the app
successfully sent an email.

Use a CoordinatorLayout

A Snackbar is attached to a view. The
Snackbar provides basic functionality
if it is attached to any object derived from the View
class, such as any of the common layout objects. However, if the
Snackbar
is attached to a CoordinatorLayout, the
Snackbar gains additional features:

The layout moves some other UI elements when the Snackbar appears. For example, if the layout
has a FloatingActionButton, the layout
moves the button up when it shows a Snackbar, instead of drawing the Snackbar on top of the button. You can see how
this looks in Figure 1.

Display a message

There are two steps to displaying a message. First, you create a Snackbar object with the message text. Then,
you call that object's show() method to display the message to the user.

Creating a Snackbar object

Create a Snackbar object by
calling the static Snackbar.make() method. When you create the Snackbar, you specify both the message it
displays, and the length of time to show the message:

Kotlin

val mySnackbar = Snackbar.make(view, stringId, duration)

Java

Snackbar mySnackbar = Snackbar.make(view, stringId, duration);

viewId

The view to attach the Snackbar to.
The method actually searches up the view hierarchy from the passed
view until it reaches either a CoordinatorLayout, or the window decor's
content view. Ordinarily, it's simplest to just pass the CoordinatorLayout enclosing your content.

stringId

The resource ID of the message you want to display. This can be formatted
or unformatted text.

Showing the message to the user

Kotlin

mySnackbar.show()

Java

mySnackbar.show();

The system does not show multiple Snackbar objects at the same time, so if the
view is currently displaying another Snackbar, the system queues your Snackbar and displays it after the current
Snackbar expires or is dismissed.

If you just want to show a message to the user and won't need to call any of
the Snackbar object's utility methods,
you don't need to keep the reference to the Snackbar after you call show(). For this reason, it's
common to use method chaining to create and show a Snackbar in one statement: