Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Add Action Snackbar in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Snackbar provides lightweight feedback about an operation. The message appears at the bottom of the screen on mobile and lowers left on larger devices. SnackBars plays a very important role in the user experience. The snack bar which may or may not contain the action button to do shows the message which had happened due to the user interaction immediately. Snackbar is similar to Toast but there are some differences such as

  • Snackbar can only be shown at the bottom of the screen.
  • It has more than one option.
  • It can be swiped off before the time limit.

An Action Snackbar can perform actions that are specified in the Snackbar within the time limit of Snackbar.

Step by Step Implementation

Step 1: Create a New Project

Create an empty activity Android Studio project. And select Kotlin as a programming language. To create a new project in Android Studio please refer to Create a new project in android studio in kotlin.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintVertical_bias="0.134" />
        android:text="Delete Message"
        app:layout_constraintTop_toTopOf="parent" />

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package com.ayush.snackbar
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout
class MainActivity : AppCompatActivity() {
    lateinit var clmain : ConstraintLayout
    lateinit var btn : Button
    override fun onCreate(savedInstanceState: Bundle?) {
        // view bindings
        clmain = findViewById(
        btn = findViewById(
        // setting click listener for an
        // event of click on the view.
        btn.setOnClickListener {
            // create an instance of the snackbar
            val snackbar = Snackbar.make(clmain, "Message is Deleted", Snackbar.LENGTH_LONG)
                .setAction("UNDO") {
                    val snackbar =
                        Snackbar.make(clmain, "Message is restored", Snackbar.LENGTH_LONG)
            // call show() method to
            // display the snackbar

So, our app is ready. And we can see the output.


My Personal Notes arrow_drop_up
Last Updated : 28 Mar, 2022
Like Article
Save Article
Similar Reads