SubscribeSubscribe with Androidlift to get new post on mail.

Android Material Design Snackbar Example

Android Material  Design snackbar Example

[divider]

This post explains Android Material Design snackbar Example of different snackbar styles.

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

Android also provides a toast, primarily used for system messaging. Toasts are similar to snackbars but do not contain actions and cannot be swiped off screen.

Usage

Show only one snackbar on screen at a time.

Placement

Snackbars appear above most elements on screen, and they are equal in elevation to the floating action button. However, they are lower in elevation than dialogs, bottom sheets, and navigation drawers.

Behavior

Upon entrance, snackbars animate upwards from the bottom edge of the screen. When they appear, they do not block input.

They exit by being swiped off-screen or automatically disappear after a timeout or user interaction elsewhere (such as summoning a new surface or activity).

Demo

Lets create project in android studio and follow the coding.

In this application we have 3 buttons as follows-

  • Simple Snackbar

simple snackbar
simple snackbar

Check below syntax for a simple snackbar.

– make(View view, CharSequence text, int duration)  –  Make a Snackbar to display a message

And duration should be

  • LENGTH_SHORT – Show the Snackbar indefinitely.
  •  LENGTH_LONG –  Show the Snackbar for a long period of time.
  • LENGTH_INDEFINITE –  Show the Snackbar indefinitely.
  • Snackbar with Action Buttons
actioncallback snackbar
actioncallback snackbar

– public Snackbar setAction (CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this Snackbar.

Parameters

text Text to display
listener callback to be invoked when the action is clicked

  • Custom Snackbar 
Android Material Design snackbar Example
Android Material Design snackbar Example

And this is the customized snackbar with backround color and textcolor along with aciton call back.

 

The complete coding is below..

activity_main.xml

MainActivity.java

colors.xml

AndroidManifest.xml

build.gradle.xml

styles.xml

 

We are done here..Now run and check the app..

Have good day…!

 

(42)