Most visited

Recently visited

Snackbar

public final class Snackbar
extends Object

java.lang.Object
   ↳ 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 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.

boolean isShownOrQueued()

Returns whether this Snackbar is currently being shown, or is queued to be shown next.

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 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.

Inherited methods

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 Snackbar.

getDuration

int getDuration ()

Return the duration.

Returns
int

See also:

getView

View getView ()

Returns the Snackbar's view.

Returns
View

isShown

boolean isShown ()

Return whether this Snackbar is currently being shown.

Returns
boolean

isShownOrQueued

boolean isShownOrQueued ()

Returns whether this Snackbar is currently being shown, or is queued to be shown next.

Returns
boolean

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 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

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

Snackbar setAction (int resId, 
                View.OnClickListener listener)

Set the action to be displayed in this Snackbar.

Parameters
resId int: String resource to display
listener View.OnClickListener: callback to be invoked when the action is clicked
Returns
Snackbar

setAction

Snackbar setAction (CharSequence text, 
                View.OnClickListener listener)

Set the action to be displayed in this Snackbar.

Parameters
text CharSequence: Text to display
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

setActionTextColor

Snackbar setActionTextColor (int color)

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

Parameters
color int
Returns
Snackbar

setCallback

Snackbar setCallback (Snackbar.Callback callback)

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

Parameters
callback Snackbar.Callback
Returns
Snackbar

setDuration

Snackbar setDuration (int duration)

Set how long to show the view for.

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

setText

Snackbar setText (int resId)

Update the text in this Snackbar.

Parameters
resId int: The new text for the Toast.
Returns
Snackbar

setText

Snackbar setText (CharSequence message)

Update the text in this Snackbar.

Parameters
message CharSequence: The new text for the Toast.
Returns
Snackbar

show

void show ()

Show the Snackbar.

Hooray!