- 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
- Enhanced Snackbar with ProgressBar for Android
- SnackProgressBar
- Features
- Getting Started
- SnackProgressBarManager
- SnackProgressBar
- Update
- Dismiss
- Set Progress
- JavaDoc
- Download
- Android snackbar with progressbar
- Enhanced Snackbar with ProgressBar for Android.
- Snackbar above BottomNavigation
- Inflate error — cannot parse TextView
- can’t access SnackProgressBar.TYPE_HORIZONTAL
- v6.1 no longer works with CoordinatorLayout, must be a FrameLayout now
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.
Источник
Enhanced Snackbar with ProgressBar for Android
SnackProgressBar
Enhanced Snackbar for Android.
Features
- Two types of ProgressBar (TYPE_HORIZONTAL and TYPE_CIRULAR) are available (see image below). It can also be used as a normal SnackBar.
- Supports multi-lines of message.
- Supports long action text by moving it to next line as per Material Design.
- Supports swipe to dimiss behaviour even without providing CoordinatorLayout. (Or you can remove this behaviour for CoordinatorLayout)
- Additional views can be added to animate with the SnackProgressBar.
- Provides OverlayLayout to prevent user input.
- Provides a queue system.
- Icon can be added.
- Supports bundle in SnackProgressBar to carry information.
- Supports changing element color and text size.
Getting Started
NOTE: v4.0 is a huge leap from v3.4 and offers much better flexibility. Please take note when migrating.
SnackProgressBarManager
Start by creating an instance of SnackProgressBarManager in your activity.
If possible, the root view of the activity should be provided and can be any type of layout.
SnackProgressBar
Create a SnackProgressBar by calling the following examples.
Note that action can be inserted in every type of SnackProgressBar.
Show the SnackProgressBar by calling:
To enable callback for OnShown and OnDismissed , include a onDisplayId when calling show() .
Or you can add the SnackProgressBar into memory and call it later.
Calling show() will put the SnackProgressBar into a queue, which will be shown after those in queue before it has been dismissed
(by user action or set showDuration).
Note: If LENGTH_INDEFINITE is specified for the queued SnackProgressBar, adding a new SnackProgressBar into the queue will cause
the previous SnackProgressBar to use LENGTH_SHORT instead, dismissed and then show the new SnackProgressBar.
Update
Calling show() will always animate the hiding and showing of SnackProgressBar between queue. Use updateTo() instead to modify the
displayed SnackProgressBar without animation. To modify the currently showing SnackProgressBar:
Dismiss
Call snackProgressBarManager.dismiss() to dismiss the currently showing SnackProgressBar. The next SnackProgressBar in queue will be shown.
Call snackProgressBarManager.dismissAll() to dismiss the currently showing SnackProgressBar and clear all other SnackProgressBars in queue.
Set Progress
Call snackProgressBarManager.setProgress() to set the progress of ProgressBar.
JavaDoc
Download
In the project Gradle:
In the app Gradle:
NOTE: The latest java version is v2.2 and may include unfixed bugs. Higher versions are in Kotlin. The DemoActivity is still in Java.
Источник
Android snackbar with progressbar
Enhanced Snackbar with ProgressBar for Android.
Please do not use v6.0 and v6.1 due to issue #20 and #23.
- v6.4.0 added ‘Rounded Corner Background’ as per latest Material Design! To use it, call SnackProgressBarManager.useRoundedCornerBackground(true) . It is false by default.
- v6.1.1+ does not break migration from v5.0. Added the following features:
- SnackProgressBarLayout is now public, so you can directly edit the layout in OnDisplayListener.onLayoutInflated .
- WeakReference is now used for views to prevent memory leak.
- LifeCycle Architecture is used to call SnackProgressBarManager.disable() automatically in OnDestroy to prevent memory leak.
- v5.0 is a migration to androidx from v4.1. There is no change in all methods. Only use this if you have migrated.
- v4.1 is a huge leap from v3.4 and offers much better flexibility. Snackbar types changed to TYPE_NORMAL , TYPE_HORIZONTAL , TYPE_CIRCULAR .
- v3.4 is using Snackbar types of TYPE_ACTION , TYPE_MESSAGE , TYPE_DETERMINATE , TYPE_INDETERMINATE .
- Two types of ProgressBar (TYPE_HORIZONTAL and TYPE_CIRULAR) are available (see image below). It can also be used as a normal SnackBar.
- Supports multi-lines of message.
- Supports long action text by moving it to next line as per Material Design.
- Supports swipe to dimiss behaviour even without providing CoordinatorLayout. (Or you can remove this behaviour for CoordinatorLayout)
- Additional views can be added to animate with the SnackProgressBar.
- Provides OverlayLayout to prevent user input.
- Provides a queue system.
- Icon can be added.
- Supports bundle in SnackProgressBar to carry information.
- Supports changing element color and text size.
NOTE: This library is still following the old Material Design Specifications.
You must have colorAccent declared in colors.xml file in your res folder. This library needs this value to apply proper styling to SnackProgressBar. Example:
Start by creating an instance of SnackProgressBarManager in your activity. If possible, the root view of the activity should be provided and can be any type of layout. Starting v6.0, you can provide a LifecycleOwner of an activity / fragment. This will call SnackProgressBarManager.disable() in OnDestroy to prevent memory leak.
Create a SnackProgressBar by calling the following examples.
Note that action can be inserted in every type of SnackProgressBar.
Show the SnackProgressBar by calling:
You can include an onDisplayId when calling show() .
Or you can add the SnackProgressBar into memory and call it later.
Calling show() will put the SnackProgressBar into a queue. It will be shown after other queued SnackProgressBars.
Note: Starting v6.2.0, if LENGTH_INDEFINITE is specified for the queued SnackProgressBar, adding a new SnackProgressBar into the queue will cause the previous SnackProgressBar to be dismissed after showing completely. Prior to that, the SnackProgressBar will be dismissed after LENGTH_SHORT and then show the new SnackProgressBar, but this is counter-intuitive and causes issue #26 and #28.
Calling show() will always animate the hiding and showing of SnackProgressBar between queue. Use updateTo() instead to modify the displayed SnackProgressBar without animation. To modify the currently showing SnackProgressBar:
Источник
Enhanced Snackbar with ProgressBar for Android.
Enhanced Snackbar with ProgressBar for Android.
Please do not use v6.0 and v6.1 due to issue #20 and #23.
- v6.4.0 added ‘Rounded Corner Background’ as per latest Material Design! To use it, call SnackProgressBarManager.useRoundedCornerBackground(true) . It is false by default.
- v6.1.1+ does not break migration from v5.0. Added the following features:
- SnackProgressBarLayout is now public, so you can directly edit the layout in OnDisplayListener.onLayoutInflated .
- WeakReference is now used for views to prevent memory leak.
- LifeCycle Architecture is used to call SnackProgressBarManager.disable() automatically in OnDestroy to prevent memory leak.
- v5.0 is a migration to androidx from v4.1. There is no change in all methods. Only use this if you have migrated.
- v4.1 is a huge leap from v3.4 and offers much better flexibility. Snackbar types changed to TYPE_NORMAL , TYPE_HORIZONTAL , TYPE_CIRCULAR .
- v3.4 is using Snackbar types of TYPE_ACTION , TYPE_MESSAGE , TYPE_DETERMINATE , TYPE_INDETERMINATE .
- Two types of ProgressBar (TYPE_HORIZONTAL and TYPE_CIRULAR) are available (see image below). It can also be used as a normal SnackBar.
- Supports multi-lines of message.
- Supports long action text by moving it to next line as per Material Design.
- Supports swipe to dimiss behaviour even without providing CoordinatorLayout. (Or you can remove this behaviour for CoordinatorLayout)
- Additional views can be added to animate with the SnackProgressBar.
- Provides OverlayLayout to prevent user input.
- Provides a queue system.
- Icon can be added.
- Supports bundle in SnackProgressBar to carry information.
- Supports changing element color and text size.
NOTE: This library is still following the old Material Design Specifications.
You must have colorAccent declared in colors.xml file in your res folder. This library needs this value to apply proper styling to SnackProgressBar. Example:
Start by creating an instance of SnackProgressBarManager in your activity. If possible, the root view of the activity should be provided and can be any type of layout. Starting v6.0, you can provide a LifecycleOwner of an activity / fragment. This will call SnackProgressBarManager.disable() in OnDestroy to prevent memory leak.
Create a SnackProgressBar by calling the following examples.
Note that action can be inserted in every type of SnackProgressBar.
Show the SnackProgressBar by calling:
You can include an onDisplayId when calling show() .
Or you can add the SnackProgressBar into memory and call it later.
Calling show() will put the SnackProgressBar into a queue. It will be shown after other queued SnackProgressBars.
Note: Starting v6.2.0, if LENGTH_INDEFINITE is specified for the queued SnackProgressBar, adding a new SnackProgressBar into the queue will cause the previous SnackProgressBar to be dismissed after showing completely. Prior to that, the SnackProgressBar will be dismissed after LENGTH_SHORT and then show the new SnackProgressBar, but this is counter-intuitive and causes issue #26 and #28.
Calling show() will always animate the hiding and showing of SnackProgressBar between queue. Use updateTo() instead to modify the displayed SnackProgressBar without animation. To modify the currently showing SnackProgressBar:
Call snackProgressBarManager.dismiss() to dismiss the currently showing SnackProgressBar. The next SnackProgressBar in queue will be shown. Call snackProgressBarManager.dismissAll() to dismiss the currently showing SnackProgressBar and clear all other SnackProgressBars in queue.
Call snackProgressBarManager.setProgress() to set the progress of ProgressBar.
IMPORTANT: To avoid memory leak, call SnackProgressBarManager.disable() in onDestroy manually. Else, provide a LifecycleOwner in the constructor of SnackProgressBarManager to automatically call this method.
In the project Gradle:
In the app Gradle:
Copyright 2017-2019 Saw Ting Yik
Licensed under the Apache License, Version 2.0 (the «License»); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an «AS IS» BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Snackbar above BottomNavigation
How would I put the snackbar above the bottom layout bar?
the viewsToMove method would move the bottom bar, however, I want to snackbar to be above the bottom navigation bar.
The standard approach (with standard snackbars) is to set a margin on the bottom, but this doesn’t seem to be possible with snackprogressbar.
Inflate error — cannot parse TextView
version: 6.1 (apparently, it’s also broken)
Looks like something related to the TextView on SnackProgressBar.
can’t access SnackProgressBar.TYPE_HORIZONTAL
as the title says
After updating to v6.0, I can’t set
Also, there’s a lot of compile-time errors. IMHO, v6.0 isn’t stable
v6.1 no longer works with CoordinatorLayout, must be a FrameLayout now
Prior to v6.x, I was using a CoordinatorLayout as the view for the snackbar since I have a FAB that I would like to respond to a snackbar being shown. Upon updating, I’ve had to use FrameLayout to avoid a crash.
I’ve also tried to access snackProgressBarLayout.setViewsToMove$lib_release(View[]); , but it has no effect when getting the FAB view.
This is my error: ClassCastException: androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
The error is fixed when I change the coordinatorLayout to a FrameLayout, but the FAB no longer moves along with it. I’ve tried making a CoordinatorLayout the parent of the FrameLayout, but the same error results. I’ve tried android:animateLayoutChanges=»true» , but I don’t think that has any effect on a FrameLayout.
You’ve done a tremendous job with 6.0+, loving the new behavior to .updateTo and the public SnackProgressBarLayout, as well as Lifecycle management.
Источник