Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

Snackbar

public final class Snackbar
extends BaseTransientBottomBar<Snackbar>

java.lang.Object
   ↳ com.google.android.material.snackbar.BaseTransientBottomBar<com.google.android.material.snackbar.Snackbar>
     ↳ com.google.android.material.snackbar.Snackbar


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 ERROR(/#setAction(CharSequence, android.view.View.OnClickListener)).

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

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.

Inherited constants

From class com.google.android.material.snackbar.BaseTransientBottomBar

Inherited fields

From class com.google.android.material.snackbar.BaseTransientBottomBar

Public methods

void dismiss()

Dismiss the BaseTransientBottomBar.

boolean isShown()

Return whether this BaseTransientBottomBar 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(CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Snackbar setAction(int resId, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Snackbar setActionTextColor(ColorStateList colors)

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

Snackbar setCallback(Snackbar.Callback callback)

This method is deprecated. Use addCallback(BaseCallback)

Snackbar setText(CharSequence message)

Update the text in this Snackbar.

Snackbar setText(int resId)

Update the text in this Snackbar.

void show()

Show the BaseTransientBottomBar.

Inherited methods

From class com.google.android.material.snackbar.BaseTransientBottomBar
From class java.lang.Object

Constants

LENGTH_INDEFINITE

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)

LENGTH_LONG

int LENGTH_LONG

Show the Snackbar for a long period of time.

See also:

Constant Value: 0 (0x00000000)

LENGTH_SHORT

int LENGTH_SHORT

Show the Snackbar for a short period of time.

See also:

Constant Value: -1 (0xffffffff)

Public methods

dismiss

void dismiss ()

Dismiss the BaseTransientBottomBar.

isShown

boolean isShown ()

Return whether this BaseTransientBottomBar is currently being shown.

Returns
boolean

make

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.

Parameters
view View: The view to find a parent from.

resId int: The resource id of the string resource to use. Can be formatted text.

duration int: How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

Returns
Snackbar

make

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.

Parameters
view View: The view to find a parent from.

text CharSequence: The text to show. Can be formatted text.

duration int: How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

Returns
Snackbar

setAction

Snackbar setAction (CharSequence text, 
                View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Parameters
text CharSequence: Text to display for the action

listener View.OnClickListener: callback to be invoked when the action is clicked

Returns
Snackbar

setAction

Snackbar setAction (int resId, 
                View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Parameters
resId int: String resource to display for the action

listener View.OnClickListener: callback to be invoked when the action is clicked

Returns
Snackbar

setActionTextColor

Snackbar setActionTextColor (ColorStateList colors)

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

Parameters
colors ColorStateList

Returns
Snackbar

setCallback

Snackbar setCallback (Snackbar.Callback callback)

This method is deprecated.
Use addCallback(BaseCallback)

Set a callback to be called when this the visibility of this Snackbar changes. Note that this method is deprecated and you should use addCallback(BaseCallback) to add a callback and removeCallback(BaseCallback) to remove a registered callback.

Parameters
callback Snackbar.Callback: Callback to notify when transient bottom bar events occur.

Returns
Snackbar

setText

Snackbar setText (CharSequence message)

Update the text in this Snackbar.

Parameters
message CharSequence: The new text for this BaseTransientBottomBar.

Returns
Snackbar

setText

Snackbar setText (int resId)

Update the text in this Snackbar.

Parameters
resId int: The new text for this BaseTransientBottomBar.

Returns
Snackbar

show

void show ()

Show the BaseTransientBottomBar.