Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snackbar is not dismissing on swipe

i have a snackbar in my appcompat activity. It has a button OK which dismiss the snackbar.It is working perfact. but i can't dismiss the snackbar on swipe(left to right).

Following is my code for snackbar....

final Snackbar snackbar = Snackbar
                                .make(view, "Error Message", Snackbar.LENGTH_INDEFINITE);

                        snackbar.setAction("OK", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                            snackbar.dismiss();
                                    }
                                });

                        snackbar.show();

Edit 1

I have Relative layout as parent layout in my activity's XML layout.

like image 904
Intimate Avatar asked Aug 08 '16 07:08

Intimate


4 Answers

Snackbar needs a CoordinatorLayout as its root layout or some where on top of it, to perform its various operations like swipe to dismiss. You need to have that some where in your layout hierarchy.

Further the view that we pass in the Snackbar.make() method is used to search a CoordinatorLayout some where in the view hierarchy. The method traverse from this view to the root view to find a CoordinatorLayout over which it can show the snackbar and perform its animations and operations.

So try replacing root layout to CoordinatorLayout and your problem will be solved.

like image 156
Ashwani Kumar Avatar answered Oct 21 '22 02:10

Ashwani Kumar


As a reference to Ashwani Kumars answer. I saw Intimate asked if there is a way to implement this with LinearLayout. Well simple wrap your original LinearLayout with a CoordinatorLayout with match_parent in android:layout_height and android:layout_width attributes.

this will keep your original arrangement and still make sure the Snackbar is swipable.

Snackbar will now look like this:

For fragments -

Snackbar.make(getView(), "Error Message", Snackbar.LENGTH_INDEFINITE);

For activities -

Snackbar.make(this.findViewById(android.R.id.content), "Error Message",  Snackbar.LENGTH_INDEFINITE);

Assuming you wraped your whole layout with CoordinatorLayout and this is the root layout.

like image 31
Nimig Avatar answered Oct 21 '22 02:10

Nimig


I've written a library that supports swipe to dimiss behaviour even without providing CoordinatorLayout. Users can swipe both left or right to dismiss it (you can only swipe to right originally). It also includes progressBar and other stuff. Try it out https://github.com/tingyik90/snackprogressbar.

All you need to do is to create a SnackProgressBar and allow swipe to dismiss. Sample code:

SnackProgressBar messageType = new SnackProgressBar(
        SnackProgressBar.TYPE_MESSAGE, "Your message")
        .setSwipeToDismiss(true)
like image 35
tingyik90 Avatar answered Oct 21 '22 01:10

tingyik90


Snackbars in my GLSurfaceView game don't dismiss with a swipe, and users may not know to swipe anyway if they did. The following one line of code dismisses a Snackbar with any touch of the bar. Critically I found if the user does happen to hit the action button if it has one, whatever action it is set to do is still performed. The overall dismiss does not get in the way.

snackbar.getView().setOnClickListener(view -> snackbar.dismiss());
like image 27
Androidcoder Avatar answered Oct 21 '22 03:10

Androidcoder