Skip to content

Snackbar Android widget example

  • by

Snackbar Android is introduced in Android material design. Its works the same as the Toast widget to show short-time Message or Feedback. But Snackbar also provides control on some actions (Like Undo, Retry, Cancel, etc). You can also change the color of the Snackbar and text. This message is shown at bottom of your app like the below image.

Snackbar Android widget example

In this tutorial, you will learn how to use Snackbar in the Android app with an example.

How to use Android Snackbar :

Create a new Android project (How to create a new project)

Step 1. Add library in gradle:app (ProjectName>app>build.gradle(app))
compile ''

Android design library 24.2.0 (version always changing by the Android team so always update Android SDK and Tools)

The complete code for adding a dependency in build.gradle

apply plugin: ''

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId ""
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
  testCompile 'junit:junit:4.12'
compile ''
compile ''
Step 2. Add following code in app>src>main>res>layout>”activity_main.xml”

CoordinatorLayout is acting as a container of performing child view action.

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

        android:text="Show My SnackBar" />

Step 3. Add following code in app>src>main>java>””

Performing an action on the button to open a snackbar, with onclicklisteners and showing a message in toast a confirmation.


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        final CoordinatorLayout coordinatorLayout=(CoordinatorLayout)findViewById(;
        Button btn_sneakbar=(Button)findViewById(;
        btn_sneakbar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Snackbar snackbar=Snackbar.make(coordinatorLayout,"Welcome Eyehunt",Snackbar.LENGTH_LONG)
                        .setAction("Undo", new View.OnClickListener() {//On click Undo
                            public void onClick(View view) {
                                Toast.makeText(MainActivity.this," Undo ",Toast.LENGTH_LONG).show();
Step 4. Run the application, in the emulator or On your Android device

Output Snackbar Android example Screenshot

Snackbar Android widget example

Snackbar Android example Source code link

Development environment

Android Studio : 2.1.3
compileSdkVersion 24
buildToolsVersion “24.0.1”
minSdkVersion 15 (Ice Cream Sandwich)
targetSdkVersion 24 (Nougat)

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading