Snackbar
public
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.
Snackbars can contain an action which is set via setAction(CharSequence, android.view.View.OnClickListener)
.
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 Snackbar.Callback
via BaseTransientBottomBar.addCallback(BaseCallback)
.
For more information, see the component developer guidance and design guidelines.
Summary
Nested classes | |
---|---|
class |
Snackbar.Callback
Callback class for |
Inherited constants |
---|
Inherited fields |
---|
Public methods | |
---|---|
void
|
dismiss()
Dismiss the |
int
|
getDuration()
Return the duration. |
boolean
|
isShown()
Return whether this |
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
|
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
|
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
|
setAction(CharSequence text, View.OnClickListener listener)
Set the action to be displayed in this |
Snackbar
|
setActionTextColor(ColorStateList colors)
Sets the text color of the action specified in |
Snackbar
|
setActionTextColor(int color)
Sets the text color of the action specified in |
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
|
setBackgroundTintMode(PorterDuff.Mode mode)
|
Snackbar
|
setCallback(Snackbar.Callback callback)
This method is deprecated.
Use |
Snackbar
|
setMaxInlineActionWidth(int width)
Sets the max width of the action to be in the same line as the message. |
Snackbar
|
setText(int resId)
Update the text in this |
Snackbar
|
setText(CharSequence message)
Update the text in this |
Snackbar
|
setTextColor(int color)
Sets the text color of the message specified in |
Snackbar
|
setTextColor(ColorStateList colors)
Sets the text color of the message specified in |
Snackbar
|
setTextMaxLines(int maxLines)
Sets the max line count of the message specified in |
void
|
show()
Show the |
Protected methods | |
---|---|
static
boolean
|
hasSnackbarButtonStyleAttr(Context context)
This method is deprecated. This is for backward compatibility with AppCompat themes. |
Inherited methods | |
---|---|
Public methods
getDuration
public int getDuration ()
Return the duration.
Returns | |
---|---|
int |
isShown
public boolean isShown ()
Return whether this BaseTransientBottomBar
is currently being shown.
Returns | |
---|---|
boolean |
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 BaseTransientBottomBar.setAnchorView(int) . |
text |
CharSequence : The text to show. Can be formatted text. |
duration |
int : How long to display the message. Can be BaseTransientBottomBar.LENGTH_SHORT , BaseTransientBottomBar.LENGTH_LONG , BaseTransientBottomBar.LENGTH_INDEFINITE , or a custom duration in milliseconds.
|
Returns | |
---|---|
Snackbar |
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 BaseTransientBottomBar.setAnchorView(int) . |
text |
CharSequence : The text to show. Can be formatted text. |
duration |
int : How long to display the message. Can be BaseTransientBottomBar.LENGTH_SHORT , BaseTransientBottomBar.LENGTH_LONG , BaseTransientBottomBar.LENGTH_INDEFINITE , or a custom duration in milliseconds.
|
Returns | |
---|---|
Snackbar |
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. |
resId |
int : The resource id of the string resource to use. Can be formatted text. |
duration |
int : How long to display the message. Can be BaseTransientBottomBar.LENGTH_SHORT , BaseTransientBottomBar.LENGTH_LONG , BaseTransientBottomBar.LENGTH_INDEFINITE , or a custom duration in milliseconds.
|
Returns | |
---|---|
Snackbar |
setAction
public 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
public 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
public Snackbar setActionTextColor (ColorStateList colors)
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener)
.
Parameters | |
---|---|
colors |
ColorStateList |
Returns | |
---|---|
Snackbar |
setActionTextColor
public Snackbar setActionTextColor (int color)
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener)
.
Parameters | |
---|---|
color |
int |
Returns | |
---|---|
Snackbar |
setBackgroundTint
public Snackbar setBackgroundTint (int color)
Sets the tint color of the background Drawable.
Parameters | |
---|---|
color |
int |
Returns | |
---|---|
Snackbar |
setBackgroundTintList
public Snackbar setBackgroundTintList (ColorStateList colorStateList)
Sets the tint color state list of the background Drawable.
Parameters | |
---|---|
colorStateList |
ColorStateList |
Returns | |
---|---|
Snackbar |
setBackgroundTintMode
public Snackbar setBackgroundTintMode (PorterDuff.Mode mode)
Parameters | |
---|---|
mode |
PorterDuff.Mode |
Returns | |
---|---|
Snackbar |
setCallback
public Snackbar setCallback (Snackbar.Callback callback)
This method is deprecated.
Use BaseTransientBottomBar.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 BaseTransientBottomBar.addCallback(BaseCallback)
to add a
callback and BaseTransientBottomBar.removeCallback(BaseCallback)
to remove a registered callback.
Parameters | |
---|---|
callback |
Snackbar.Callback : Callback to notify when transient bottom bar events occur. |
Returns | |
---|---|
Snackbar |
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.
Parameters | |
---|---|
width |
int |
Returns | |
---|---|
Snackbar |
setText
public Snackbar setText (int resId)
Update the text in this Snackbar
.
Parameters | |
---|---|
resId |
int : The new text for this BaseTransientBottomBar .
|
Returns | |
---|---|
Snackbar |
setText
public Snackbar setText (CharSequence message)
Update the text in this Snackbar
.
Parameters | |
---|---|
message |
CharSequence : The new text for this BaseTransientBottomBar .
|
Returns | |
---|---|
Snackbar |
setTextColor
public Snackbar setTextColor (int color)
Sets the text color of the message specified in setText(CharSequence)
and setText(int)
.
Parameters | |
---|---|
color |
int |
Returns | |
---|---|
Snackbar |
setTextColor
public Snackbar setTextColor (ColorStateList colors)
Sets the text color of the message specified in setText(CharSequence)
and setText(int)
.
Parameters | |
---|---|
colors |
ColorStateList |
Returns | |
---|---|
Snackbar |
setTextMaxLines
public Snackbar setTextMaxLines (int maxLines)
Sets the max line count of the message specified in setText(CharSequence)
and setText(int)
.
Parameters | |
---|---|
maxLines |
int |
Returns | |
---|---|
Snackbar |
Protected methods
hasSnackbarButtonStyleAttr
protected static boolean hasSnackbarButtonStyleAttr (Context context)
This method is deprecated.
This is for backward compatibility with AppCompat themes.
Snackbar
s 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.
Parameters | |
---|---|
context |
Context |
Returns | |
---|---|
boolean |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-12-15 UTC.