How to add a Snackbar in Android
Last Updated :
13 Feb, 2025
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.
Step by Step Implementation:
Step 1: Add necessary dependencies
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.
implementation ("com.google.android.material:material:1.12.0")
Step 2: Working with activity_main.xml
Now add the following code in the activity_main.xml file. It will create a button named Open Snackbar.
activity_main.xml:
XML
<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"
android:background="@color/white"
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>
Design UI:
Step 3: Working with MainActivity file
Now add the following code in the MainActivity 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 File:
MainActivity.java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity {
private Button button;
private 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(v -> {
// Create a snackbar
Snackbar snackbar = Snackbar
.make(layout, "Message is deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO", view ->
Toast.makeText(
MainActivity.this,
"Undo Clicked",
Toast.LENGTH_SHORT
)
.show());
snackbar.show();
});
}
}
MainActivity.kt
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.snackbar.Snackbar
class MainActivity : AppCompatActivity() {
private lateinit var button: Button
private lateinit var layout: CoordinatorLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button = findViewById(R.id.button)
layout = findViewById(R.id.layout)
button.setOnClickListener {
// Create a snackbar
val snackbar = Snackbar
.make(layout, "Message is deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO") {
Toast.makeText(
this@MainActivity,
"Undo Clicked",
Toast.LENGTH_SHORT
)
.show()
}
snackbar.show()
}
}
}
Output: