Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to add a Snackbar in Android

  • Difficulty Level : Hard
  • Last Updated : 23 Feb, 2021

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.


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  1. 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 ''
  2. Now add the following code in the activity_main.xml file. It will create a button named Open Snackbar.


    <?xml version="1.0" encoding="utf-8"?>
            android:text="Open Snackbar"

  3. Now add the following code in the 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.

    package org.geeksforgeeks.gfgsnackbar;
    import androidx.coordinatorlayout
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    public class MainActivity
        extends AppCompatActivity {
        Button button;
        CoordinatorLayout layout;
        protected void onCreate(
            Bundle savedInstanceState)
            button = findViewById(;
            layout = findViewById(;
                new View.OnClickListener() {
                    public void onClick(View v)
                        // Create a snackbar
                        Snackbar snackbar
                            = Snackbar
                                      "Message is deleted",
                                      // If the Undo button
                                      // is pressed, show
                                      // the message using Toast
                                      new View.OnClickListener() {
                                          public void onClick(View view)
                                                      "Undo Clicked",


My Personal Notes arrow_drop_up
Recommended Articles
Page :