How to add a Snackbar in Android

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.
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'}Now add the following code in the activity_main.xml file. It will create a button named Open Snackbar.activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/layout"    tools:context=".MainActivity">      <Button        android: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>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.javaMainActivity.javapackage org.geeksforgeeks.gfgsnackbar;  import androidx.appcompat.app.AppCompatActivity;import androidx.coordinatorlayout    .widget.CoordinatorLayout;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;  import com.google.android.material    .snackbar    .Snackbar;  public class MainActivity    extends AppCompatActivity {      Button button;    CoordinatorLayout layout;      @Override    protected void onCreate(        Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          button = findViewById(R.id.button);        layout = findViewById(R.id.layout);          button.setOnClickListener(            new View.OnClickListener() {                @Override                public void onClick(View v)                {                      // Create a snackbar                    Snackbar snackbar                        = Snackbar                              .make(                                  layout,                                  "Message is deleted",                                  Snackbar.LENGTH_LONG)                              .setAction(                                  "UNDO",                                    // If the Undo button                                  // is pressed, show                                  // the message using Toast                                  new View.OnClickListener() {                                      @Override                                      public void onClick(View view)                                      {                                          Toast                                              .makeText(                                                  MainActivity.this,                                                  "Undo Clicked",                                                  Toast.LENGTH_SHORT)                                              .show();                                      }                                  });                      snackbar.show();                }            });    }}
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'}
dependencies {    implementation 'com.google.android.material:material:1.1.0'}
Now add the following code in the activity_main.xml file. It will create a button named Open Snackbar.activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/layout"    tools:context=".MainActivity">      <Button        android: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>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/layout"    tools:context=".MainActivity">      <Button        android: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>
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.javaMainActivity.javapackage org.geeksforgeeks.gfgsnackbar;  import androidx.appcompat.app.AppCompatActivity;import androidx.coordinatorlayout    .widget.CoordinatorLayout;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;  import com.google.android.material    .snackbar    .Snackbar;  public class MainActivity    extends AppCompatActivity {      Button button;    CoordinatorLayout layout;      @Override    protected void onCreate(        Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          button = findViewById(R.id.button);        layout = findViewById(R.id.layout);          button.setOnClickListener(            new View.OnClickListener() {                @Override                public void onClick(View v)                {                      // Create a snackbar                    Snackbar snackbar                        = Snackbar                              .make(                                  layout,                                  "Message is deleted",                                  Snackbar.LENGTH_LONG)                              .setAction(                                  "UNDO",                                    // If the Undo button                                  // is pressed, show                                  // the message using Toast                                  new View.OnClickListener() {                                      @Override                                      public void onClick(View view)                                      {                                          Toast                                              .makeText(                                                  MainActivity.this,                                                  "Undo Clicked",                                                  Toast.LENGTH_SHORT)                                              .show();                                      }                                  });                      snackbar.show();                }            });    }}
MainActivity.java
package org.geeksforgeeks.gfgsnackbar;  import androidx.appcompat.app.AppCompatActivity;import androidx.coordinatorlayout    .widget.CoordinatorLayout;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;  import com.google.android.material    .snackbar    .Snackbar;  public class MainActivity    extends AppCompatActivity {      Button button;    CoordinatorLayout layout;      @Override    protected void onCreate(        Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          button = findViewById(R.id.button);        layout = findViewById(R.id.layout);          button.setOnClickListener(            new View.OnClickListener() {                @Override                public void onClick(View v)                {                      // Create a snackbar                    Snackbar snackbar                        = Snackbar                              .make(                                  layout,                                  "Message is deleted",                                  Snackbar.LENGTH_LONG)                              .setAction(                                  "UNDO",                                    // If the Undo button                                  // is pressed, show                                  // the message using Toast                                  new View.OnClickListener() {                                      @Override                                      public void onClick(View view)                                      {                                          Toast                                              .makeText(                                                  MainActivity.this,                                                  "Undo Clicked",                                                  Toast.LENGTH_SHORT)                                              .show();                                      }                                  });                      snackbar.show();                }            });    }}
Output:
Android-Bars
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference Between Implicit Intent and Explicit Intent in Android
How to Create and Add Data to SQLite Database in Android?
Retrofit with Kotlin Coroutine in Android
Navigation Drawer in Android
Broadcast Receiver in Android With Example
Arrays in Java
Arrays.sort() in Java with examples
Reverse a string in Java
Split() String method in Java with examples
Queue Interface In Java