Snackbar

public class Snackbar extends BaseTransientBottomBar


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.

Snackbars can contain an action which is set via setAction.

Snackbars automatically disappear after a timeout. They can also be dismissed by being swiped off screen, by action click, from a new snackbar being displayed, or manually via a call to dismiss.

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

For more information, see the component developer guidance and design guidelines.

Summary

Nested types

Callback class for Snackbar instances.

Public methods

void

Dismiss the BaseTransientBottomBar.

int

Return the duration.

boolean

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

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

Make a Snackbar to display a message

Snackbar
setAction(int resId, OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Snackbar
setAction(CharSequence text, OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Snackbar
setActionTextColor(int color)

Sets the text color of the action specified in setAction.

Snackbar
setActionTextColor(ColorStateList colors)

Sets the text color of the action specified in setAction.

Snackbar
setBackgroundTint(int color)

Sets the tint color of the background Drawable.

Snackbar
setBackgroundTintList(ColorStateList colorStateList)

Sets the tint color state list of the background Drawable.

Snackbar
Snackbar

This method is deprecated.

Use addCallback

Snackbar

Sets the max width of the action to be in the same line as the message.

Snackbar

Update the text in this Snackbar.

Snackbar
setText(int resId)

Update the text in this Snackbar.

Snackbar
setTextColor(int color)

Sets the text color of the message specified in setText and setText.

Snackbar
setTextColor(ColorStateList colors)

Sets the text color of the message specified in setText and setText.

Snackbar
setTextMaxLines(int maxLines)

Sets the max line count of the message specified in setText and setText.

void

Show the BaseTransientBottomBar.

Protected methods

static boolean
hasSnackbarButtonStyleAttr(Context context)

This method is deprecated.

This is for backward compatibility with AppCompat themes.

Inherited Constants

From com.google.android.material.snackbar.BaseTransientBottomBar
static final int

Animation mode that corresponds to the fade in and out animations.

static final int

Animation mode that corresponds to the slide in and out animations.

static final int

Show the Snackbar indefinitely.

static final int

Show the Snackbar for a long period of time.

static final int

Show the Snackbar for a short period of time.

Inherited methods

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

Adds the specified callback to the list of callbacks that will be notified of transient bottom bar events.

void
dispatchDismiss(int event)
View

Returns the anchor view for this BaseTransientBottomBar.

int

Returns the animation mode.

BaseTransientBottomBar.Behavior

Return the behavior.

Context

Returns the BaseTransientBottomBar's context.

SwipeDismissBehavior<View>
int
View

Returns the BaseTransientBottomBar's view.

boolean

Snackbars should still work with AppCompat themes, which don't specify a snackbarStyle.

boolean

Returns whether the anchor view layout listener is enabled.

boolean

Returns whether this bottom bar should adjust it's position based on the system gesture area on Android Q and above.

boolean

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

B

Removes the specified callback from the list of callbacks that will be notified of transient bottom bar events.

B
setAnchorView(int anchorViewId)

Sets the view the BaseTransientBottomBar should be anchored above by id.

B
setAnchorView(View anchorView)

Sets the view the BaseTransientBottomBar should be anchored above.

void
setAnchorViewLayoutListenerEnabled(
    boolean anchorViewLayoutListenerEnabled
)

Sets whether the anchor view layout listener is enabled.

B
setAnimationMode(int animationMode)

Sets the animation mode.

B

Sets the BaseTransientBottomBar.Behavior to be used in this .

B
setDuration(int duration)

Set how long to show the view for.

B
setGestureInsetBottomIgnored(boolean gestureInsetBottomIgnored)

Sets whether this bottom bar should adjust it's position based on the system gesture area on Android Q and above.

Public methods

dismiss

public void dismiss()

Dismiss the BaseTransientBottomBar.

getDuration

public int getDuration()

Return the duration.

See also
setDuration

isShown

public boolean isShown()

Return whether this BaseTransientBottomBar is currently being shown.

make

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.

Parameters
View view

The view to find a parent from.

int resId

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

int duration

How long to display the message. Can be LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE, or a custom duration in milliseconds.

make

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.

Parameters
View view

The view to find a parent from. This view is also used to find the anchor view when calling setAnchorView.

CharSequence text

The text to show. Can be formatted text.

int duration

How long to display the message. Can be LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE, or a custom duration in milliseconds.

make

public static Snackbar make(Context context, 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
Context context

The context to use to create the Snackbar view.

View view

The view to find a parent from. This view is also used to find the anchor view when calling setAnchorView.

CharSequence text

The text to show. Can be formatted text.

int duration

How long to display the message. Can be LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE, or a custom duration in milliseconds.

setAction

public Snackbar setAction(int resId, OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Parameters
int resId

String resource to display for the action

OnClickListener listener

callback to be invoked when the action is clicked

setAction

public Snackbar setAction(CharSequence text, OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Parameters
CharSequence text

Text to display for the action

OnClickListener listener

callback to be invoked when the action is clicked

setActionTextColor

public Snackbar setActionTextColor(int color)

Sets the text color of the action specified in setAction.

setActionTextColor

public Snackbar setActionTextColor(ColorStateList colors)

Sets the text color of the action specified in setAction.

setBackgroundTint

public Snackbar setBackgroundTint(int color)

Sets the tint color of the background Drawable.

setBackgroundTintList

public Snackbar setBackgroundTintList(ColorStateList colorStateList)

Sets the tint color state list of the background Drawable.

setBackgroundTintMode

public Snackbar setBackgroundTintMode(Mode mode)

setCallback

public Snackbar setCallback(Snackbar.Callback callback)

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 to add a callback and removeCallback to remove a registered callback.

Parameters
Snackbar.Callback callback

Callback to notify when transient bottom bar events occur.

setMaxInlineActionWidth

public Snackbar setMaxInlineActionWidth(int width)

Sets the max width of the action to be in the same line as the message. If the width is exceeded the action would go to the next line.

setText

public Snackbar setText(CharSequence message)

Update the text in this Snackbar.

Parameters
CharSequence message

The new text for this BaseTransientBottomBar.

setText

public Snackbar setText(int resId)

Update the text in this Snackbar.

Parameters
int resId

The new text for this BaseTransientBottomBar.

setTextColor

public Snackbar setTextColor(int color)

Sets the text color of the message specified in setText and setText.

setTextColor

public Snackbar setTextColor(ColorStateList colors)

Sets the text color of the message specified in setText and setText.

setTextMaxLines

public Snackbar setTextMaxLines(int maxLines)

Sets the max line count of the message specified in setText and setText.

show

public void show()

Show the BaseTransientBottomBar.

Protected methods

hasSnackbarButtonStyleAttr

protected static boolean hasSnackbarButtonStyleAttr(Context context)

Snackbars should still work with AppCompat themes, which don't specify a snackbarButtonStyle. This method helps to check if a valid snackbarButtonStyle is set within the current context, so that we know whether we can use the attribute.