Android – Snackbar setAction

An action could be set to Snackbar using Snackbar.setAction() method. setAction takes View.OnClickListener as second argument and you may write your set of instructions to perform the action.

In this tutorial, we will learn how to display Snackbar widget on even of an action.

The following screenshot is an example for Snackbar, with a message and an actionable item next to it.

Android Snackbar SetAction

Important Note : To use Snackbar in your android application, you need to include the ‘com.android.support.design’ package in your build.gradle (app) dependencies. There are different available versions of the package. You need to include the one with version matching the compileSdkVersion.

For example :

android {
    compileSdkVersion 26
    ...
}

dependencies {
    ...
    implementation 'com.android.support:design:26.1.0'
    ...
}

Following variants are available for setAction method.

1. setAction(int resId, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

2. setAction(CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

ADVERTISEMENT

Example – Android Snackbar – Set Action

In this example, we shall set text “DISMISS” to appear in Snackbar and an action would be taken when clicked on it.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#DDDDDD"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:background="#FFFFFF"
        android:textAllCaps="false"
        android:padding="10sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Snackbar"
         />

</LinearLayout>

MainActivity.kt

package com.tutorialkart.snackbarexample

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn.setOnClickListener {
            val snack = Snackbar.make(it,"This is a simple Snackbar",Snackbar.LENGTH_LONG)
            snack.setAction("DISMISS", View.OnClickListener {
                // executed when DISMISS is clicked
                System.out.println("Snackbar Set Action - OnClick.")
            })
            snack.show()
        }
    }
}

Run this Android application, and an Android screen will appear as shown below with a button. Click on the button “Show Snackbar”. Snackbar appears at the bottom of the screen.

Android Snackbar SetAction

Conclusion

In this Kotlin Android Tutorial, we have learnt to set an Action Listener to the Snackbar using setAction() method. In our next tutorial, we shall learn to change the text color and background of Snackbar and Action String in it.