ZOFTINO.COM android and web dev tutorials

Displaying Andorid App Messages Using Snackbar

There are many situations in android apps where you want to display messages to users without impacting the usability of the screen which user is working on. Android provided message widget called Snackbar can be used to accomplish that objective.

Snackbar lets you display simple text messages or actionable messages to users. Snackbar messages are displayed at bottom of the screen. Messages are displayed for a while before they go away. While snackbar message is shown, user can continue to work with app and current screen or activity is interactive. Only one snackbar message is shown at a time.

”snackbar

Snackbar message example

Snackbar is part of design support library. So in order to use snackbar, you need to import design support library. You can do so by updating support registry in android studio sdk manager and addng below dependecy to build.gradle file.

compile 'com.android.support:design:25.0.1'

You need to call Snackbar make method passing view, message and message duration as parameters. View parameter is used as starting point in your view hierarchy to find suitable parent to attach Snackbar to. Snackbar’s make method tries to find CoordinatorLayout view or the window decor's content view using passed view and makes it as parent for Snackbar.

If snackbar is attached to CoordinatorLayout, it gets certain features. So having CoordinatorLayout in view hierarchy provides additional features such as swipe and auto adjust of widgets when snackbar message is displayed.

You can add actions to snackbar message by using Snackbar’s setAction method and defining action listener for the action button on snackbar message.

Below example app main screen displays a button, on clicking the button you will see snackbar message. The example snackbar has action which starts new activity on clicking it.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zoftino.snackbar">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CouponsActivity">

        </activity>
    </application>

</manifest>

MainActivity

package com.zoftino.snackbar;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void showSBMessage(View view){
        Snackbar cpnSnackbar = Snackbar.make(findViewById(R.id.activity_main), R.string.snackbar_msg, Snackbar.LENGTH_LONG);

        cpnSnackbar.setAction(R.string.view_cpn, new SnackbarActionListener(this));
        cpnSnackbar.show();
    }
}

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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
tools:context="com.zoftino.snackbar.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Snackbar Message"></TextView>

    <Button
        android:text="View Message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="showSBMessage"
        android:id="@+id/button"></Button>
</LinearLayout>

Snackbaractionlistener

package com.zoftino.snackbar;

import android.content.Context;
import android.content.Intent;
import android.view.View;

public class SnackbarActionListener implements View.OnClickListener{
    Context mContext;
    SnackbarActionListener(Context curContext){
        mContext = curContext;
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(mContext, CouponsActivity.class);
        mContext.startActivity(intent);
    }
    public Context getmContext() {
        return mContext;
    }
}