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.
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 |
Inherited constants |
---|
![]()
com.google.android.material.snackbar.BaseTransientBottomBar
|
Inherited fields |
---|
![]()
com.google.android.material.snackbar.BaseTransientBottomBar
|
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(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 |
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 | |
---|---|
![]()
com.google.android.material.snackbar.BaseTransientBottomBar
| |
![]()
java.lang.Object
|
Public methods
getDuration
int getDuration ()
Return the duration.
Returns | |
---|---|
int |
isShown
boolean isShown ()
Return whether this BaseTransientBottomBar
is currently being shown.
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.
Parameters | |
---|---|
view |
View : The view to find a parent from. This view is also used to find the anchor view when
calling setAnchorView(int) . |
text |
CharSequence : The text to show. Can be formatted text. |
duration |
int : How long to display the message. Can be LENGTH_SHORT , LENGTH_LONG , LENGTH_INDEFINITE , or a custom duration in milliseconds.
|
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.
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 LENGTH_SHORT , LENGTH_LONG , LENGTH_INDEFINITE , or a custom duration in milliseconds.
|
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 |
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 |
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 |
setBackgroundTint
Snackbar setBackgroundTint (int color)
Sets the tint color of the background Drawable.
Parameters | |
---|---|
color |
int |
Returns | |
---|---|
Snackbar |
setBackgroundTintList
Snackbar setBackgroundTintList (ColorStateList colorStateList)
Sets the tint color state list of the background Drawable.
Parameters | |
---|---|
colorStateList |
ColorStateList |
Returns | |
---|---|
Snackbar |
setBackgroundTintMode
Snackbar setBackgroundTintMode (PorterDuff.Mode mode)
Parameters | |
---|---|
mode |
PorterDuff.Mode |
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 |
setMaxInlineActionWidth
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
Snackbar setText (int resId)
Update the text in this Snackbar
.
Parameters | |
---|---|
resId |
int : The new text for this BaseTransientBottomBar .
|
Returns | |
---|---|
Snackbar |
setText
Snackbar setText (CharSequence message)
Update the text in this Snackbar
.
Parameters | |
---|---|
message |
CharSequence : The new text for this BaseTransientBottomBar .
|
Returns | |
---|---|
Snackbar |
setTextColor
Snackbar setTextColor (int color)
Sets the text color of the message specified in setText(CharSequence)
and setText(int)
.
Parameters | |
---|---|
color |
int |
Returns | |
---|---|
Snackbar |
setTextColor
Snackbar setTextColor (ColorStateList colors)
Sets the text color of the message specified in setText(CharSequence)
and setText(int)
.
Parameters | |
---|---|
colors |
ColorStateList |
Returns | |
---|---|
Snackbar |
Protected methods
hasSnackbarButtonStyleAttr
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 is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-07-20 UTC.