Android Snackbar Example Tutorial

In this tutorial we’ll discuss and implement various forms of Android Snackbar widget in our application.

Android Snackbar

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast.

Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.

Difference between Toast and Snackbar

  1. A Toast messages can be customised and printed anywhere on the screen, but a Snackbar can be only showed in the bottom of the screen
  2. A Toast message don’t have action button, but Snackbar may have action button optionally. Though, A Snackbar shouldn’t have more than one action button
  3. Toast message cannot be off until the time limit finish, but Snackbar can be swiped off before the time limit

Note: Toast message and Snackbar have display length property in common.

A code snippet to display a basic android Snackbar is shown below.

In the above snippet make() method accepts three parameters:

  1. coordinatorLayout : It is the root layout of the activity
  2. : This is the message to be appear on snackbar, and we can customise it with our own message
  3. Snackbar.LENGH_LONG : This is last parameter which is the time limit how long snackbar to be displayed

show() method is used to display the Snackbar on the screen.

Android Snackbar Example Project Structure


Android Snackbar Example Code

No changes in the activity_main.xml code which contains the CoordinatorLayout.

The content_main.xml consists of three buttons. One for each type of Snackbar that we’ll be discussing.

The code snippet for Action Call Snackbar button is given below:

In the above code a new onClickListener method is invoked on clicking the action button with the respective Snackbar being displayed in it.

The code snippet for Custom Snackbar that’s invoked on the second button is given below:

The is given below.

The activity_main.xml is unchanged.

The output of the snackbar android app in action is shown below.
snackbar android app example

This brings an end to this tutorial. You can download the final Android Snackbar project from the link below.

Reference: Android Developer Doc

By admin

Leave a Reply

%d bloggers like this: