Snackbar Android Example Using Design Support Library

Snackbar is the best replacement for toast messages & you can set actions in the form of pop up.

Snackbar Example

Most of the lollipop material design apps uses snack bar.

To use this functionality for all the versions of Android from 2.1 onward, update design library  dependency in gradle file.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
}

To show a simple text on Snackbar just use below code.

   FabButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar.make(v,"This is sample snack bar text",Snackbar.LENGTH_SHORT).show();
            }
        });

To customize the position of snackbar,  just add a CoordinatorLayout & this will be input view for snackbar.

For eg: to show snackbar on top of the screen.

Custom snackbar top of application
Set custom layout to snackbar.

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/snackbarlocation">
    </android.support.design.widget.CoordinatorLayout>

This is to set actions for snackbar & click listener

 CoordinatorLayout Clayout = (CoordinatorLayout)findViewById(R.id.snackbarlocation);

        FabButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar.make(Clayout, "This snack bar located at top", Snackbar.LENGTH_SHORT).show();
            }
        });
 Snackbar.make(Clayout, "This snack bar located at top", Snackbar.LENGTH_LONG).
                        setAction("Retry",snackbarClickListener).show();
 View.OnClickListener snackbarClickListener = new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         
     }
 };

 

To Show snackbar just below the FloatingActionButton, just add the buttons in CoordinatorLayout. On Click set the parent view as input, Refer below code

Snackbar below the Floating Action Button

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/add_fab_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/abc_ic_ab_back_mtrl_am_alpha"
            android:layout_gravity="end|bottom"
            app:borderWidth="0dp"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_fab_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ic_ab_back_mtrl_am_alpha"
        android:layout_gravity="bottom|left"
        app:borderWidth="0dp"/>

    </android.support.design.widget.CoordinatorLayout>