How to dismiss a Snackbar using it's own Action button?

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.


Solution 1:

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()

Solution 2:

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();

Solution 3:

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

Solution 4:

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.