added in version 22.2.0
belongs to Maven artifact com.android.support:design:27.1.0

Snackbar

public final class Snackbar
extends BaseTransientBottomBar<Snackbar>

java.lang.Object
   ↳ android.support.design.widget.BaseTransientBottomBar<android.support.design.widget.Snackbar>
     ↳ android.support.design.widget.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 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 android.support.design.widget.BaseTransientBottomBar

Public methods

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.

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

Make a Snackbar to display a message.

Snackbar setAction(int resId, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Snackbar setAction(CharSequence text, 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 setActionTextColor(int color)

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

Snackbar setCallback(Snackbar.Callback callback)

This method was deprecated in API level 26.1.0. Use addCallback(BaseCallback)

Snackbar setText(CharSequence message)

Update the text in this Snackbar.

Snackbar setText(int resId)

Update the text in this Snackbar.

Inherited methods

From class android.support.design.widget.BaseTransientBottomBar
From class java.lang.Object

Constants

LENGTH_INDEFINITE

added in version 23.4.0
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

added in version 22.2.0
int LENGTH_LONG

Show the Snackbar for a long period of time.

See also:

Constant Value: 0 (0x00000000)

LENGTH_SHORT

added in version 22.2.0
int LENGTH_SHORT

Show the Snackbar for a short period of time.

See also:

Constant Value: -1 (0xffffffff)

Public methods

make

added in version 22.2.0
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 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

make

added in version 22.2.0
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 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

setAction

added in version 22.2.0
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

setAction

added in version 22.2.0
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

setActionTextColor

added in version 23.4.0
Snackbar setActionTextColor (ColorStateList colors)

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

Parameters
colors ColorStateList

Returns
Snackbar

setActionTextColor

added in version 23.4.0
Snackbar setActionTextColor (int color)

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

Parameters
color int

Returns
Snackbar

setCallback

added in version 23.4.0
Snackbar setCallback (Snackbar.Callback callback)

This method was deprecated in API level 26.1.0.
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

added in version 22.2.0
Snackbar setText (CharSequence message)

Update the text in this Snackbar.

Parameters
message CharSequence: The new text for this BaseTransientBottomBar.

Returns
Snackbar

setText

added in version 22.2.0
Snackbar setText (int resId)

Update the text in this Snackbar.

Parameters
resId int: The new text for this BaseTransientBottomBar.

Returns
Snackbar