Last Updated:

SnackBar in Android : Creating a custom SnackBar | Example

In this tutorial, we'll look at an example of creating and configuring different widget forms in a simple application.



Table of Contents


Brief description of SnackBar in Android

SnackBar is a Material Design library widget to replace a pop-up message.Toast

SnackBar is a simple and functional widget that is used to display messages at the bottom of the application. In addition, this widget may contain an additional programmable button for various actions.

Difference Between Toast and SnackBar

  • Toast messages can be customized anywhere on the screen, and the SnackBar can only be shown at the bottom of the screen.
  • There is no way to add controls to Toast messages, and the SnackBar widget can contain a single button.
  • Toast messages cannot be closed until the time specified by the programmer is over, and the SnackBar can be closed at any time.

Example of creating a SnackBar widget

To create a widget in Android, you need to use the layout method with three parameters:SnackBarmake()


  • In the code above, the coordLayout parameter is the root element of the activity.
  • The option is a text message that will be displayed in the SnackBar
  • Snackbar.LENGH_LONG is how long the message will be displayed on the screen.

The call to the method will display the message composed by the method on the

But we can make it easier if we don't need a link from myAwesomeSnackbar to the created one:SnackBar


Example of using snackBar in Android


Create a new project in Android Studio using the example, but use Basic Activity instead of Empty Activity:


After creating the project, you will have 2 layouts in the res/layout folder:

  • content_main.xml with the root RelativeLayout and TextView.
  • activity_main.xml is with the root CoordinatorLayout, Toolbar and FloatingActionButton (here it is not given, since we do not change anything in it. at the end of the article you can download the project with all the files).

In the layout of the content_main.xml, let's add 3 Button widgets to test three different ways to customize the widget:SnackBar

File strings.xml with labels on the buttons:

Now let's write the initialization of our buttons in the MainActivity class method and add different widget layouts by clicking on each button.onCreate()SnackBar

Now let's launch the project and see what happens:

and a button with a custom SnackBar widget.

As you can see, the material design library introduces a new, more convenient way to notify the user about something and even cancel some of their actions. It not only looks prettier, but also more functional than the usual Toast.SnackBar