page.title=Building and Displaying a Pop-Up Message page.tags="Snackbar" "popup" "pop-up" helpoutsWidget=true trainingnavtop=true @jd:body
You can use a {@link android.support.design.widget.Snackbar} to display a brief message to the user. The message automatically goes away after a short period. A {@link android.support.design.widget.Snackbar} is ideal for brief messages that the user doesn't necessarily need to act on. For example, an email app could use a {@link android.support.design.widget.Snackbar} to tell the user that the app successfully sent an email.
A {@link android.support.design.widget.Snackbar} is attached to a view. The {@link android.support.design.widget.Snackbar} provides basic functionality if it is attached to any object derived from the {@link android.view.View} class, such as any of the common layout objects. However, if the {@link android.support.design.widget.Snackbar} is attached to a {@link android.support.design.widget.CoordinatorLayout}, the {@link android.support.design.widget.Snackbar} gains additional features:
The {@link android.support.design.widget.CoordinatorLayout} class provides a superset of the functionality of {@link android.widget.FrameLayout}. If your app already uses a {@link android.widget.FrameLayout}, you can just replace that layout with a {@link android.support.design.widget.CoordinatorLayout} to enable the full {@link android.support.design.widget.Snackbar} functionality. If your app uses other layout objects, the simplest thing to do is wrap your existing layout elements in a {@link android.support.design.widget.CoordinatorLayout}, as in this example:
<android.support.design.widget.CoordinatorLayout android:id="@+id/myCoordinatorLayout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Here are the existing layout elements, now wrapped in a CoordinatorLayout --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- …Toolbar, other layouts, other elements… --> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
Make sure to set an android:id
tag for your {@link
android.support.design.widget.CoordinatorLayout}. You need the layout's ID
when you display the message.
Figure 1. The {@link android.support.design.widget.CoordinatorLayout} moves the {@link android.support.design.widget.FloatingActionButton} up when the {@link android.support.design.widget.Snackbar} appears.
There are two steps to displaying a message. First, you create a {@link android.support.design.widget.Snackbar} object with the message text. Then, you call that object's {@link android.support.design.widget.Snackbar#show show()} method to display the message to the user.
Create a {@link android.support.design.widget.Snackbar} object by calling the static {@link android.support.design.widget.Snackbar#make Snackbar.make()} method. When you create the {@link android.support.design.widget.Snackbar}, you specify both the message it displays, and the length of time to show the message:
Snackbar mySnackbar = Snackbar.make(viewId, stringId, duration);
Once you have created the {@link android.support.design.widget.Snackbar}, call its {@link android.support.design.widget.Snackbar#show show()} method to display the {@link android.support.design.widget.Snackbar} to the user:
mySnackbar.show();
The system does not show multiple {@link android.support.design.widget.Snackbar} objects at the same time, so if the view is currently displaying another {@link android.support.design.widget.Snackbar}, the system queues your {@link android.support.design.widget.Snackbar} and displays it after the current {@link android.support.design.widget.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 {@link android.support.design.widget.Snackbar} object's utility methods, you don't need to keep the reference to the {@link android.support.design.widget.Snackbar} after you call {@link android.support.design.widget.Snackbar#show show()}. For this reason, it's common to use method chaining to create and show a {@link android.support.design.widget.Snackbar} in one statement:
Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.email_sent, Snackbar.LENGTH_SHORT) .show();