public final class Snackbar extends Object
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)
.
Modifier and Type | Class and Description |
---|---|
static class |
Snackbar.Callback
Callback class for
Snackbar instances. |
static interface |
Snackbar.Duration |
static class |
Snackbar.SnackbarLayout |
Modifier and Type | Field and Description |
---|---|
static int |
LENGTH_INDEFINITE
Show the Snackbar indefinitely.
|
static int |
LENGTH_LONG
Show the Snackbar for a long period of time.
|
static int |
LENGTH_SHORT
Show the Snackbar for a short period of time.
|
Modifier and Type | Method and Description |
---|---|
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(CharSequence text,
View.OnClickListener listener)
Set the action to be displayed in this
Snackbar . |
Snackbar |
setAction(int resId,
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(CharSequence message)
Update the text in this
Snackbar . |
Snackbar |
setText(int resId)
Update the text in this
Snackbar . |
void |
show()
Show the
Snackbar . |
public static final int LENGTH_INDEFINITE
shown
until either it is dismissed, or another Snackbar is shown.setDuration(int)
,
Constant Field Valuespublic static final int LENGTH_SHORT
setDuration(int)
,
Constant Field Valuespublic static final int LENGTH_LONG
setDuration(int)
,
Constant Field Values@NonNull public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int duration)
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
.
view
- The view to find a parent from.text
- The text to show. Can be formatted text.duration
- How long to display the message. Either LENGTH_SHORT
or LENGTH_LONG
@NonNull public static Snackbar make(@NonNull View view, @StringRes int resId, int duration)
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
.
view
- The view to find a parent from.resId
- The resource id of the string resource to use. Can be formatted text.duration
- How long to display the message. Either LENGTH_SHORT
or LENGTH_LONG
@NonNull public Snackbar setAction(@StringRes int resId, View.OnClickListener listener)
Snackbar
.resId
- String resource to displaylistener
- callback to be invoked when the action is clicked@NonNull public Snackbar setAction(CharSequence text, View.OnClickListener listener)
Snackbar
.text
- Text to displaylistener
- callback to be invoked when the action is clicked@NonNull public Snackbar setActionTextColor(ColorStateList colors)
setAction(CharSequence, View.OnClickListener)
.@NonNull public Snackbar setActionTextColor(int color)
setAction(CharSequence, View.OnClickListener)
.@NonNull public Snackbar setText(@NonNull CharSequence message)
Snackbar
.message
- The new text for the Toast.@NonNull public Snackbar setText(@StringRes int resId)
Snackbar
.resId
- The new text for the Toast.@NonNull public Snackbar setDuration(int duration)
duration
- either be one of the predefined lengths:
LENGTH_SHORT
, LENGTH_LONG
, or a custom duration
in milliseconds.public int getDuration()
setDuration(int)
public void show()
Snackbar
.public void dismiss()
Snackbar
.@NonNull public Snackbar setCallback(Snackbar.Callback callback)
Snackbar
changes.public boolean isShown()
Snackbar
is currently being shown.public boolean isShownOrQueued()
Snackbar
is currently being shown, or is queued to be
shown next.