Snackbar provides lightweight feedback about an operation. The message appears at the bottom of the screen on mobile and lower left on larger devices. Snackbar appears above all the elements of the screen. But no component is affected by it. Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets. Snackbar is similar to Toast but the only major difference is that an action can be added with Snackbar.
Approach:
-
Add the support Library in build.gradle file and add Material Design dependency in the dependencies section.It is a part of Material Design that’s why we have to add a dependency.
dependencies {implementation 'com.google.android.material:material:1.1.0'}chevron_rightfilter_none - Now add the following code in the activity_main.xml file. It will create a button named Open Snackbar.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/layout"tools:context=".MainActivity"><Buttonandroid:layout_gravity="center"android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textAllCaps="false"android:text="Open Snackbar"/></androidx.coordinatorlayout.widget.CoordinatorLayout>chevron_rightfilter_none - 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. This snackbar contains an action and if clicked will show a toast.
MainActivity.java
packageorg.geeksforgeeks.gfgsnackbar;importandroidx.appcompat.app.AppCompatActivity;importandroidx.coordinatorlayout.widget.CoordinatorLayout;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.Toast;importcom.google.android.material.snackbar.Snackbar;publicclassMainActivityextendsAppCompatActivity {Button button;CoordinatorLayout layout;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = findViewById(R.id.button);layout = findViewById(R.id.layout);button.setOnClickListener(newView.OnClickListener() {@OverridepublicvoidonClick(View v){// Create a snackbarSnackbar snackbar= Snackbar.make(layout,"Message is deleted",Snackbar.LENGTH_LONG).setAction("UNDO",// If the Undo button// is pressed, show// the message using ToastnewView.OnClickListener() {@OverridepublicvoidonClick(View view){Toast.makeText(MainActivity.this,"Undo Clicked",Toast.LENGTH_SHORT).show();}});snackbar.show();}});}}chevron_rightfilter_none
Output:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Android | How to add Radio Buttons in an Android Application?
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
- Difference between Android 1.0 and Android 7.1
- Difference between Android 1.0 and Android 2.3
- Difference between Android 1.0 and Android 2.2
- Difference between Android 1.0 and Android 1.6
- Difference between Android 1.0 and Android 2.1
- Difference between Android 1.0 and Android 2.0.1
- Difference between Android 1.0 and Android 2.0
- Difference between Android 1.0 and Android 1.5
- Difference between Android 1.0 and Android 2.3.3
- Difference between Android 1.0 and Android 4.2.1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

