- Kotlin Android – Snackbar – Set Action – Example
- Android – Snackbar setAction
- Example – Android Snackbar – Set Action
- Conclusion
- Android Snackbar Example
- Difference between Toast and Snackbar
- Let’s Getting into Snackbar Android Example
- Create Simple Android Snackbar
- Android Snackbar with Action Callback
- Customizing the Android Snackbar
- Theming snackbars
- Implementing snackbar theming
- Android snackbar example
- Kotlin Android – Snackbar – Example
- Android Snackbar
- Example – Kotlin Android Snackbar
- Conclusion
- Snackbar
- Summary
- Nested classes
- Inherited constants
- Inherited fields
- Public methods
- Protected methods
- Inherited methods
- Public methods
- dismiss
- getDuration
- isShown
- setAction
- setAction
- setActionTextColor
- setActionTextColor
- setBackgroundTint
- setBackgroundTintList
- setBackgroundTintMode
- setCallback
- setMaxInlineActionWidth
- setText
- setText
- setTextColor
- setTextColor
- Protected methods
- hasSnackbarButtonStyleAttr
Kotlin Android – Snackbar – Set Action – Example
Android – Snackbar setAction
An action could be set to Snackbar using Snackbar.setAction() method. setAction takes View.OnClickListener as second argument and you may write your set of instructions to perform the action.
In this tutorial, we will learn how to display Snackbar widget on even of an action.
The following screenshot is an example for Snackbar, with a message and an actionable item next to it.
Important Note : To use Snackbar in your android application, you need to include the вЂcom.android.support.design’ package in your build.gradle (app) dependencies. There are different available versions of the package. You need to include the one with version matching the compileSdkVersion.
Following variants are available for setAction method.
1. setAction(int resId, View.OnClickListener listener)
Set the action to be displayed in this BaseTransientBottomBar.
2. setAction(CharSequence text, View.OnClickListener listener)
Set the action to be displayed in this BaseTransientBottomBar.
Example – Android Snackbar – Set Action
In this example, we shall set text “DISMISS” to appear in Snackbar and an action would be taken when clicked on it.
activity_main.xml
MainActivity.kt
Run this Android application, and an Android screen will appear as shown below with a button. Click on the button “Show Snackbar”. Snackbar appears at the bottom of the screen.
Conclusion
In this Kotlin Android Tutorial, we have learnt to set an Action Listener to the Snackbar using setAction() method. In our next tutorial, we shall learn to change the text color and background of Snackbar and Action String in it.
Источник
Android Snackbar Example
Android Snackbar is an interesting component introduced by Material Design. Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen and can be swiped off in order to dismiss them.
Difference between Toast and Snackbar
- Toast messages can be customized and printed anywhere on the screen, but a Snackbar can be only shown in the bottom of the screen.
- A Toast message doesn’t have action button, but Snackbar may have action button optionally.
- Toast message cannot be off until the time limit finish, but Snackbar can be swiped off before the time limit.
You can set how long the message will be shown using this three different values.
- Snackbar.LENGTH_LONG
- Snackbar.LENGTH_SHORT
- Snackbar.LENGTH_INDEFINITE
Let’s Getting into Snackbar Android Example
In Android Studio, Create New Project and fill all the details required to create a new project.
Open build.gradle and add Material Design dependency.
By using material dependency, you can also create components like Recyclerview, Cardview and snackbar etc. If you want to know more about other material components check below links.
Also, setup the application theme as theme.MaterialComponents.
Now, done with the project setup.
Create Simple Android Snackbar
Below is the syntax of a simple snackbar. The make function accepts three parameters. View, display message and duration of the message to be displayed.
Android Snackbar with Action Callback
You can also mention a callback interaction method using setAction() method. This allows us to take certain action when the user interacts with the snackbar.
Customizing the Android Snackbar
Snackbar comes with default white color text and #323232 background color. You can override these colors.
Theming snackbars
Snackbars support Material Theming and can be customized in terms of color and typography.
Implementing snackbar theming
Using theme attributes in res/values/styles.xml
or in code (affects only this snackbar):
Android snackbar example
Download the example from github.
Thanks for reading this post. Material design having lot of useful components like Material Button , Cardview etc. I will write more about material design in my future post.
Источник
Kotlin Android – Snackbar – Example
Android Snackbar
Android Snackbar is a material design component introduced with API 22.2.0. The functionality would resemble Android Toast, but unlike Toast, Snackbar could be dismissed by user or an action listener could be setup to respond to user actions.
Snackbar when shown, is displayed at the bottom of the screen, with default background color “#323232” and text color “#FFFFFF“. You may change the background and text color. We shall go through that in the tutorial – Snackbar – Change Background and Text Color.
The following screenshot is an example, of Snackbar sliding from from bottom of the screen, displaying a message.
Important Note : To use Snackbar in your android application, you need to include the вЂcom.android.support.design’ package in your build.gradle (app) dependencies. There are different available versions of the package. You need to include the one with version matching the compileSdkVersion.
Once you modify the build.gradle (Module:app), Android Studio asks for Gradle Sync. Sync gradle and you are good to use Snackbar in your project.
Example – Kotlin Android Snackbar
Create an Android Application with Kotlin Support and find the code for activity_main.xml layout file and MainActivity.kt provided below.
In this Kotlin Android Snackbar Example, we shall display a button, and when the button is clicked, Snackbar is displayed at the bottom of the screen.
activity_main.xml
MainActivity.kt
Run this Android Application on a physical Android Device or Android Virtual Device, and you get an Activity with the button as shown in the following screenshot. When you click on the “Show Snackbar” button, a Snackbar appears at the bottom of the screen.
An action could be set to Snackbar using Snackbar.setAction() method.
Conclusion
In this Kotlin Android Tutorial, we have learnt to include support library for Snackbar in the dependencies with Example Android Application. In our next tutorial, we shall learn to set action listener to the Snackbar.
Источник
Snackbar
java.lang.Object | ||
↳ | com.google.android.material.snackbar.BaseTransientBottomBar | |
↳ | 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.
To be notified when a snackbar has been shown or dismissed, you can provide a Snackbar.Callback via BaseTransientBottomBar.addCallback(BaseCallback) .
Summary
Nested classes
Callback class for Snackbar instances.
Inherited constants
Animation mode that corresponds to the fade in and out animations.
Animation mode that corresponds to the slide in and out animations.
Show the Snackbar indefinitely.
Show the Snackbar for a long period of time.
Show the Snackbar for a short period of time.
Inherited fields
Public methods
Return the duration.
Return whether this BaseTransientBottomBar is currently being shown.
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 .
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 .
Make a Snackbar to display a message.
Set the action to be displayed in this BaseTransientBottomBar .
Set the action to be displayed in this BaseTransientBottomBar .
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener) .
Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener) .
Sets the tint color of the background Drawable.
Sets the tint color state list of the background Drawable.
Sets the max width of the action to be in the same line as the message.
Update the text in this Snackbar .
Update the text in this Snackbar .
Sets the text color of the message specified in setText(CharSequence) and setText(int) .
Sets the text color of the message specified in setText(CharSequence) and setText(int) .
Protected methods
This method is deprecated. This is for backward compatibility with AppCompat themes.
Inherited methods
Adds the specified callback to the list of callbacks that will be notified of transient bottom bar events.
Returns the anchor view for this BaseTransientBottomBar .
Returns the animation mode.
Return the behavior.
Return the duration.
Snackbar s should still work with AppCompat themes, which don’t specify a snackbarStyle .
Returns whether the anchor view layout listener is enabled.
Returns whether this bottom bar should adjust it’s position based on the system gesture area on Android Q and above.
Return whether this BaseTransientBottomBar is currently being shown.
Returns whether this BaseTransientBottomBar is currently being shown, or is queued to be shown next.
Removes the specified callback from the list of callbacks that will be notified of transient bottom bar events.
Sets the view the BaseTransientBottomBar should be anchored above by id.
Sets the view the BaseTransientBottomBar should be anchored above.
Sets whether the anchor view layout listener is enabled.
Sets the animation mode.
Set how long to show the view for.
Sets whether this bottom bar should adjust it’s position based on the system gesture area on Android Q and above.
From class java.lang.Object
Object | clone() |
boolean | equals(Object arg0) |
void | finalize() |
final Class | getClass() |
int | hashCode() |
final void | notify() |
final void | notifyAll() |
String | toString() |
final void | wait(long arg0, int arg1) |
final void | wait(long arg0) |
final void | wait() |
Public methods
dismiss
getDuration
Return the duration.
Returns | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
int |
Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
boolean |
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. 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.
|