108

Android design support library now includes support for Snackbar.

I've used the following code to create one:

Snackbar.make(findViewById(R.id.root_layout), result, Snackbar.LENGTH_LONG)
        .setAction("Dismiss", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();

The snackbar can be dismissed by a swipe. However, I also want to dismiss it using its own Action Button (created using the setAction function).

However there doesn't seem to be any function available that can do that.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Asim
  • 6,962
  • 8
  • 38
  • 61
  • 5
    As a note to people coming here, making a "Dismiss" action on the `Snackbar` is against the [Google Material Design Guidelines](https://material.google.com/components/snackbars-toasts.html#snackbars-toasts-usage). – Loyalar Aug 08 '16 at 08:54
  • Specifically: https://material.io/design/components/snackbars.html#usage – Brian Stewart Feb 12 '19 at 18:16

8 Answers8

171

For Java,

The .make method returns a Snackbar object. Save an instance of that object by making it final. Then, in the onClick(), call .dismiss:

final Snackbar snackBar = Snackbar.make(findViewById(android.R.id.content), "Snackbar Message", Snackbar.LENGTH_LONG);

        snackBar.setAction("Action Message", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Call your action method here
                snackBar.dismiss();
            }
        });
        snackBar.show();

For Kotlin,

        Snackbar.make(
            findViewById(android.R.id.content),
            "Snackbar Message",
            Snackbar.LENGTH_INDEFINITE
        ).setAction("Action Message") {
            // Call action functions here
        }.show()
Community
  • 1
  • 1
EE66
  • 4,601
  • 1
  • 17
  • 20
  • 2
    if i do this in a coordinator layout with a FloatingActionButton, the FloatingActionButton does not come down. – Vinay W Jul 01 '15 at 06:20
  • 69
    A snackbar is dismissed by default when clicking the action. Explicitly calling the dismiss method is unnecessary. – Mark Buikema Nov 02 '15 at 15:05
  • 11
    @MarkBuikema this feature was added later on. At the time of the question that was not the deafult behavior of the snackbar. – EE66 Nov 02 '15 at 16:19
  • 2
    JFYI, the action won't be shown if the `OnClickListener` is `null` – crgarridos Jul 12 '18 at 18:12
  • As of today, this code will call dismiss twice with different event value, one for clicking on the action and another one for calling dissmiss() programatically. – Juan Apr 06 '19 at 19:12
59

Implement a click action and let it empty . Clicking on empty click action will dismiss snackbar .

Snackbar.make(coordinatorLayoutView, "Service Enabled", Snackbar.LENGTH_LONG)
                        .setAction("DISMISS", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            }
                        })
                        .show();
Ali Zarei
  • 3,523
  • 5
  • 33
  • 44
  • 1
    you can pass `null` for a listener, no need to create an actual instance – ccpizza Jun 15 '20 at 15:38
  • @ccpizza it's not completely the same thing: with an empty listener, you still get the button "DISMISS", but with "null" it does not display the button (just tested it myself). Edit: and it was stated in the comments of the accepted answer before you commented on this one ;) – V4karian Apr 11 '23 at 17:11
11

When you use Snackbar.LENGTH_LONG you do not need action button for dismiss , after second automatically dismiss. You should use this code :

 Snackbar snackbar = Snackbar.make(relativeLayout, "Your Message", Snackbar.LENGTH_INDEFINITE);
            snackbar.setAction("dismiss", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    snackbar.dismiss();
                }
            });

            snackbar.show();

Be careful this line :

Snackbar.LENGTH_INDEFINITE
hamed golbahar
  • 139
  • 1
  • 8
9

This is an old question, but I just want to share my own experience around similar feature on Snackbar. So we got a design for our app, that snackbar should be shown indefinitely and user should be able to dismiss it.. but there shouldn't be DISMISS button inside it (Google is not recommending Dismiss or Cancel actions inside snackbars anyway). Our snackbar had to be dismissed just by tapping on it.

The only solution, working for us, was in the end (I am using retrolambda here, but standard View.OnClickListener could be used as well):

final Snackbar snack = ... /* create proper snackbar as alway */
snack.getView().setOnClickListener(v -> snack.dismiss());

Note getView() call in the middle.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Where do you read that "Google is not recommending Dismiss or Cancel actions inside Snackbars" ? I would like to read the reason.... – JoseF Sep 14 '17 at 09:35
  • @JoseF Please check first comment under the question itself ;) It is in Material Design guidelines: https://material.io/guidelines/components/snackbars-toasts.html#snackbars-toasts-usage ("0-1 actions, not dismiss or cancel") – Dariusz Wiechecki Sep 15 '17 at 09:52
  • This answer is unique in that it gives a way to dismiss the snackbar that's set to 'LENGTH_INDEFINITE' (so user has time to read it) without having to start whatever action the action button starts. I use a Snackbar to notify my players an update has been downloaded and to restart if they want it implemented. If they aren't ready to restart I wanted a way to dismiss it without hitting the action button. Snackbars on my test devices don't dismiss with a swipe. This code dismisses it with a touch outside the action button, which should be the default behavior in my opinion – Androidcoder Nov 01 '20 at 16:11
4

Snackbar (from 'com.android.support:design:23.2.1') support many types of dismiss action. You can create a simple filter by using event, such as in this example:

Snackbar.make(view, wornMessage, Snackbar.LENGTH_LONG).setActionTextColor(context.getResources().getColor(R.color.primary))
    .setCallback(new Snackbar.Callback() {
        @Override
        public void onShown(Snackbar snackbar) {
            super.onShown(snackbar);
        // when snackbar is showing
        }

        @Override
        public void onDismissed(Snackbar snackbar, int event) {
            super.onDismissed(snackbar, event);
            if (event != DISMISS_EVENT_ACTION) {
               //will be true if user not click on Action button (for example: manual dismiss, dismiss by swipe
            }
        }
    })
    .setAction("Undo, view1 -> {
        // if user click on Action button
}).show();

Snackbar's dismiss types:

/** Indicates that the Snackbar was dismissed via a swipe.*/
public static final int DISMISS_EVENT_SWIPE = 0;
/** Indicates that the Snackbar was dismissed via an action click.*/
public static final int DISMISS_EVENT_ACTION = 1;
/** Indicates that the Snackbar was dismissed via a timeout.*/
public static final int DISMISS_EVENT_TIMEOUT = 2;
/** Indicates that the Snackbar was dismissed via a call to {@link #dismiss()}.*/
public static final int DISMISS_EVENT_MANUAL = 3;
/** Indicates that the Snackbar was dismissed from a new Snackbar being shown.*/
public static final int DISMISS_EVENT_CONSECUTIVE = 4;

P.S. In sample code used lambda expressions (by RetroLambda)

Tunaki
  • 132,869
  • 46
  • 340
  • 423
a.black13
  • 2,157
  • 4
  • 19
  • 20
2

I had the same problem. When I used .dismiss() the animations looked different and there were two problems:

  1. FAB does not go down again
  2. the SnackBar itself does not slide down like it would on a Click

Looking into the original Android Source Code for Snackbar I found the following solution:

View snackbarView = snackbar.getView();
Button snackbarActionButton = (Button) snackbarView.findViewById(android.support.design.R.id.snackbar_action);
//snackbarActionButton.setSoundEffectsEnabled(false); // might be considered in order not to have a confusing sound because nothing was clicked by the user

Then, I would call the .performClick on the snackBarActionButton

snackBarActionButton.performClick();

Link to the Android Source Code for Snackbar: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/design/src/android/support/design/widget/Snackbar.java

0

There is a simple Kotlin extention function:

fun View.showSnack(msg: String) {
    Snackbar.make(this, msg, Snackbar.LENGTH_LONG).apply {
        setAction(resources.getString(android.R.string.cancel)) {
            dismiss()
        }
        setActionTextColor(resources.getColor(android.R.color.holo_red_light, null))
        show()
    }
}
MeLean
  • 3,092
  • 6
  • 29
  • 43
-1

in case you want to put the snackbar in a static method, use this.

 public static  void showSnackbar(Activity activity,String msg){
        Snackbar snackBar = null;

        snackBar = Snackbar.make( ((activity.findViewById(android.R.id.content))), msg,20000);//Snackbar.LENGTH_INDEFINITE
        Snackbar finalSnackBar = snackBar;
        //
        View.OnClickListener action =  new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finalSnackBar.dismiss();
            }
        };

        snackBar.setAction("OKAY",action )
                .setActionTextColor(activity.getApplicationContext().getResources().getColor(android.R.color.holo_red_light ));
        snackBar.show();
    }