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 'com.android.support:design:24.2.0'

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: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "eyehunt.in.materialdesign.snackbar"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
  testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
}
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"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="eyehunt.in.materialdesign.snackbar.MainActivity">

    <Button
        android:id="@+id/btn_snackbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Show My SnackBar" />

</android.support.design.widget.CoordinatorLayout>
Step 3. Add following code in app>src>main>java>”MainActivity.java”

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

package eyehunt.in.materialdesign.snackbar;

import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CoordinatorLayout coordinatorLayout=(CoordinatorLayout)findViewById(R.id.coordinatorLayout);
        Button btn_sneakbar=(Button)findViewById(R.id.btn_snackbar);
        btn_sneakbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar snackbar=Snackbar.make(coordinatorLayout,"Welcome Eyehunt",Snackbar.LENGTH_LONG)
                        .setAction("Undo", new View.OnClickListener() {//On click Undo
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(MainActivity.this," Undo ",Toast.LENGTH_LONG).show();
                            }
                        });
                snackbar.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

https://github.com/EyeHunts/SnackbarAndroid

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