Material Design Android Snackbar Tutorial

Android snackbar is an interesting component introduced by Material Design. Android snackbar replaces somehow an existing component called Toast. The features provided by this component are similar to Toast even if it introduces some user interaction. If you don’t know this component the video below shows how to use Android Snackbar:

Getting started with Android Snackbar

Before using Android snackbar, i reccomend reading this tutorial about how to develop Android weather app with Material design. The first step is getting familiar with this component. The simplest way to use this component is:

Snackbar.make(v, "Welcome to SwA", Snackbar.LENGTH_LONG).show();

This simple line of code shows a message at the bottom of the screen. You can set how long the message will be shown using this three different values:

  • Snackbar.LENGTH_LONG
  • Snackbar.LENGTH_SHORT

If you are not comfortable with these values you can set your own time, inserting a custom value. The result is shown below:

Android snackbar tutorial

Android Snackbar and Action Callback

In the previous example, we have seen that we can simply show a custom message. Android Snackbar can do more handling user interaction. We can add a custom action with the message like the figure shown below:

android snackbar callback action

In this case, the code is very simple:

Snackbar bar = Snackbar.make(v, "Weclome to SwA", Snackbar.LENGTH_LONG)
   .setAction("Dismiss", new View.OnClickListener() {
     public void onClick(View v) {
       // Handle user action

As you can see, we used setAction to set the callback action.

How to Customise Snackbar

If we want to customise this component, we can select the text color and and action color:

android snackbar custom color

In this case to set the action color we use:


while to set the message text color:

TextView tv = (TextView) bar.getView().findViewById(;

Floating Action Button and Android Snackbar

As you have noticed from the previous picture the Snackbar message is shown at the bottom of the screen. This may cause some problems when we use Floating Action Button because some parts can overlap.

android snackbar with fab

As you can notice the Snackbar overlap the Floating Action Button. In this case, we can use the CoordinatorLayout so that the FAB moves up a little bit to give space to Snackbar.

Source code available @github

At the end of this post, you know how to inform users about Android app events using Android snackbar

  • Anne

    Hi! I have an issue, I hope you can help.

    I have three apps, and they all use the same core code, so I just added that code you mentioned once. In two of them, it works perfectly, but in the third, it gives me nullpointerexception, when I try to change the text color. My debugging showed me that the snackbar is created correctly, yet when I look at the children (text and action), the text’s ID is different from the text’s ID in the other apps.. does that make sense? It’s just one letter different but the result is that when I look for the textvire using the I get null. What do you think it could be?? thanks! hope you can help.

    • Can you post the code that doesn’t work so that i can check it. It could be useful to have the layout file also. Thx

      • Anne

        The code works in two cases, but not in the third.
        This is it:

        RelativeLayout parentView = (RelativeLayout) getView().findViewById(;
        Snackbar snackbar = Snackbar
        .make(parentView, LocalizablesFacade.getString(getActivity(), “MESSAGE”), Snackbar.LENGTH_INDEFINITE)
        .setAction(LocalizablesFacade.getString(getActivity(), “CLOSE”), new View.OnClickListener() {
        public void onClick(View view) {
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(;
        textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.snack_bar_text));
        snackbar.setActionTextColor(ContextCompat.getColor(getActivity(), R.color.snack_bar_action));
        sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.snack_bar_background));;

        The parentView is the parent layout of the fragment, and im calling this method on the onActivityCreated method. But I don’t think the code is an issue, because it works in other cases, but on this case it doesn’t.. what could it be? something wrong with the library?

        • Garuav

          Hi All

          I think Snackbar is only work with CoordinatorLayout

          • No you can use it without CoordinatorLayout but if you FAB you should pay attention

  • Mehraj Malik

    Glad to know that you have many Certificates in java(Sun certified). I just have one ,
    Very good blog.Please keep it up.

  • vinodh

    how can we set snackbar permanently to the layout. if onclick present don’t hide the snackbar. permanently fixed to the layout. sorry i am new to android. any one please help me.

    • If you want to set the snackbar permanently I guess this component doesn’t fit you.
      Snackbar is made to give information to the user for a specific time interval.
      Of course, you make this time lasting for long but not permanently.
      Hope it helps you!

      • vinodh

        thank you sir. In recent, i saw this technique fassos app. my app also same like e commerce. so that’s way i asked. Thanking you sir….

  • Michael

    I would not recommend using the snackbar within a RealtiveLayout. When the user has a smartphone with bottom navigation bar (Nexus devices) the snackbar will be hidden behind this bar.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.