When migrating a project or switching between branches which are and aren't migrated, Android Studio cannot build projects because it cannot find the android.support.design.widget.Snackbar
package. Support/Design packages are removed but the migration table does not list the correct new package for this component.
Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.
The duration should be LENGTH_SHORT, LENGTH_LONG or LENGTH_INDEFINITE. When LENGTH_INDEFINITE is used, the snackbar will be displayed indefinite time and can be dismissed with swipe off. And you can set the duration of your snackbar by setDuration(int) method.
This example demonstrates how to use Snackbar in Android Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
This example demonstrates how do I use snackBar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 – Open build.gradle (Module app) add the following dependancy -
Now add the following code in the MainActivity.java file. This will define the button and add a onClickListener to the button. In the onClickListener a Snackbar is created and is called. So whenever the button is clicked, the onClickListener creates a snackbar and calls it and the user sees the message.
The Snackbar action button should be used for lightweight user interaction. i.e. instantaneous actions. In conclusion, Snackbar is very handy and useful for alerting the user over an action recently performed, and providing an option for reversal of recently performed action.
In this tutorial we will explore simple step by step SnackBar examples. What is a SnackBar? Snackbars are part of the material design android widgets and do 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.
This took a long time to resolve, as until your project successfully builds, autocomplete will not function to lookup packages.
The correct package to use is:
com.google.android.material.snackbar.Snackbar
It is also important to remember to change this on the XML tags as well as the imports if the migration has not worked successfully.
Old build artifact com.android.support:design
now solves in com.google.android.material:material:1.0.0
for AndroidX build artifact.
Add implementation "com.google.android.material:material:1.1.0"
to your app-level build.gradle
.
Also, ensure that your project is set up to migrate old packages to Androidx by including this in your gradle.properties
file:
android.useAndroidX=true
android.enableJetifier=true
Import
com.google.android.material.snackbar.Snackbar;
instead of
android.support.design.widget.Snackbar;
if you shifted to androidx and AS couldn't find the package anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With