Android APIs
public final class

Snackbar

extends Object
java.lang.Object
   ↳ android.support.design.widget.Snackbar

Class Overview

Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen.

Snackbars can contain an action which is set via setAction(CharSequence, android.view.View.OnClickListener).

To be notified when a snackbar has been shown or dismissed, you can provide a Snackbar.Callback via setCallback(Callback).

Summary

Nested Classes
class Snackbar.Callback Callback class for Snackbar instances. 
Constants
int LENGTH_INDEFINITE Show the Snackbar indefinitely.
int LENGTH_LONG Show the Snackbar for a long period of time.
int LENGTH_SHORT Show the Snackbar for a short period of time.
Public Methods
void dismiss()
Dismiss the Snackbar.
int getDuration()
Return the duration.
View getView()
Returns the Snackbar's view.
boolean isShown()
Return whether this Snackbar is currently being shown.
static Snackbar make(View view, int resId, int duration)
Make a Snackbar to display a message.
static Snackbar make(View view, CharSequence text, int duration)
Make a Snackbar to display a message

Snackbar will try and find a parent view to hold Snackbar's view from the value given to view.

Snackbar setAction(int resId, View.OnClickListener listener)
Set the action to be displayed in this Snackbar.
Snackbar setAction(CharSequence text, View.OnClickListener listener)
Set the action to be displayed in this Snackbar.
Snackbar setActionTextColor(ColorStateList colors)
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener).
Snackbar setActionTextColor(int color)
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener).
Snackbar setCallback(Snackbar.Callback callback)
Set a callback to be called when this the visibility of this Snackbar changes.
Snackbar setDuration(int duration)
Set how long to show the view for.
Snackbar setText(int resId)
Update the text in this Snackbar.
Snackbar setText(CharSequence message)
Update the text in this Snackbar.
void show()
Show the Snackbar.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int LENGTH_INDEFINITE

Show the Snackbar indefinitely. This means that the Snackbar will be displayed from the time that is shown until either it is dismissed, or another Snackbar is shown.

See Also
Constant Value: -2 (0xfffffffe)

public static final int LENGTH_LONG

Show the Snackbar for a long period of time.

See Also
Constant Value: 0 (0x00000000)

public static final int LENGTH_SHORT

Show the Snackbar for a short period of time.

See Also
Constant Value: -1 (0xffffffff)

Public Methods

public void dismiss ()

Dismiss the Snackbar.

public int getDuration ()

Return the duration.

See Also

public View getView ()

Returns the Snackbar's view.

public boolean isShown ()

Return whether this Snackbar is currently being shown.

public static Snackbar make (View view, int resId, int duration)

Make a Snackbar to display a message.

Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.

Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets like FloatingActionButton.

Parameters
view The view to find a parent from.
resId The resource id of the string resource to use. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

public static Snackbar make (View view, CharSequence text, int duration)

Make a Snackbar to display a message

Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.

Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets like FloatingActionButton.

Parameters
view The view to find a parent from.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

public Snackbar setAction (int resId, View.OnClickListener listener)

Set the action to be displayed in this Snackbar.

Parameters
resId String resource to display
listener callback to be invoked when the action is clicked

public Snackbar setAction (CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this Snackbar.

Parameters
text Text to display
listener callback to be invoked when the action is clicked

public Snackbar setActionTextColor (ColorStateList colors)

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener).

public Snackbar setActionTextColor (int color)

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener).

public Snackbar setCallback (Snackbar.Callback callback)

Set a callback to be called when this the visibility of this Snackbar changes.

public Snackbar setDuration (int duration)

Set how long to show the view for.

Parameters
duration either be one of the predefined lengths: LENGTH_SHORT, LENGTH_LONG, or a custom duration in milliseconds.

public Snackbar setText (int resId)

Update the text in this Snackbar.

Parameters
resId The new text for the Toast.

public Snackbar setText (CharSequence message)

Update the text in this Snackbar.

Parameters
message The new text for the Toast.

public void show ()

Show the Snackbar.