Snack Bar

From google material design documentation.

Snackbars provide lightweight feedback about an operation by showing a brief message at the bottom of the screen. Snackbars can contain an action.

How to add?

I. In your build.gradle add latest design library.

dependencies {  
    // optionally, Snackbar can be used in pair 
    // with CoordinatorLayout
    // compile 'com.android.support:appcompat-v7:X.X.X'

    compile 'com.android.support:design:X.X.X' // where X.X.X version
}

II. Create Snackbar instance with make() method. Then call show() method.

Snackbar  
    .make(view, "No network connection.",Snackbar.LENGTH_SHORT)
    .show();

Parameter view is used to find parent. Snackbar will be displayed over it.

Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.

Duration

To specify how long Snackbar will be visible on screen use setDuration method.

// pre defined constants
Snackbar.LENGTH_SHORT // 1500 millis  
Snackbar.LENGTH_LONG // 2750 millis  
Snackbar.LENGTH_INDEFINITE

// you can set custom duration
snackbar.setDuration(TimeUnit.MINUTES.toMillis(1));  

Dismiss

To hide Snackbar manually at any time use dismiss() method.

Snackbar snackBar = Snackbar.make(view, text, duration);  
snackBar.dismiss(); //hide snackbar  

Events

To track whenever Snackbar was shown or dismissed use setCallback method.

Snackbar  
  .make(...)
  .setCallback(new Snackbar.Callback() {
    @Override
    public void onDismissed(Snackbar snackbar, int event) {
      // do some action on dismiss
    }
    @Override
    public void onShown(Snackbar snackbar) {
      // do some action when snackbar is showed
    }
  })

Parameter event from onDismissed() is one of predefined constants in Snackbar.Callback.

Actions

Snackbar can contain an action. To add it call setAction() method.

Snackbar  
 .make(...)
 .setAction("Retry", new View.OnClickListener() {
             @Override
             public void onClick(View v) {
               // retry to send email here
             }
           })

To enable swipe-to-dismiss and automatically moving of widgets like FloatingActionButton use CoordinatorLayout as your root layout.

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

    <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:src="@android:drawable/ic_menu_call"/>
</android.support.design.widget.CoordinatorLayout>  

How to style?

With theme

I. Declare custom style in your values/styles.xml file.

<style name="SnackbarTheme" parent="Theme.AppCompat.Light">  
    <item name="colorAccent">@color/indigo</item>
    <item name="android:textColor">@color/pink</item>
</style>  

II. Apply this style to your Activity via android:theme attribute in AndroidManifest.xml file.

<activity  
    android:name=".SnackbarActivity"
    android:theme="@style/AppTheme">
</activity>  

Note: Applying theme to Activity will apply colorAccent and android:textColor to all of its views.

With code

Get Snackbar view using getView() method and change it properties.

// create instance
Snackbar snackbar = Snackbar.make(view, text, duration);

// set action button color
snackbar.setActionTextColor(getResources().getColor(R.color.indigo));

// get snackbar view
View snackbarView = snackbar.getView();

// change snackbar text color
int snackbarTextId = android.support.design.R.id.snackbar_text;  
TextView textView = (TextView)snackbarView.findViewById(snackbarTextId);  
textView.setTextColor(getResources().getColor(R.color.indigo));

// change snackbar background
snackbarView.setBackgroundColor(Color.MAGENTA);