How to keep the snackbar open after action is called
Asked Answered
T

3

10

I use a snackbar to notify the users of my app that they aren't connected to the internet. I added a "retry" action to the snackbar which re-checks the connection. I want the snackbar to stay displayed until I dismiss it myself (when an internet connection is found), but I can't get this to work. Whenever I click on the action, the snackbar dismisses.

I've set the duration to indefinite and the snackbar does stay open indefinitely but it dismisses when I click on the action.

I've read online that dismissing the snackbar automatically after clicking on the action hasn't always been the default behavior.

edit:

I feel like my question might be badly phrased. I have a snackbar with an action but I don't want the snackbar to close when the action is executed, which it automatically does atm.

Tonsillitis answered 14/5, 2016 at 18:47 Comment(4)
Why not just create a new snackbar when the user taps "retry"? IMHO it might even be good to wait a second before displaying the new snackbar as visual feedback telling the user that "yes, I retried, but there's still no internet connection". (That's not to say that there's no way of doing what you want)Supplement
So do you want to show snake bar until user manually dismiss it. right?Ethelred
Well, the user can dismiss it by swiping, which I don't have a problem with. I just don't want it to close automatically when they click on the actionTonsillitis
There already is visual feedback when checking for a connection, but I suppose I'll end up using a new snackbar like you suggested. I just can't imagine why it wouldn't be easy to just NOT close the snackbar. Like I said, this even used to be the default behavior.Tonsillitis
S
1

You can override the OnClickListener set for the button. First make the snackbar with and set the action with some dummy listener

Snackbar snackbar = Snackbar.make(view,"TXT",Snackbar.LENGTH_LONG).setAction("OK", new View.OnClickListener() {
        @Override
        public void onClick(View v) { }
    });

And then find the button and set your listner

snackbar.show();
ViewGroup group = (ViewGroup) snackbar.getView();
for(int i=0; i< group.getChildCount();i++){
        View v = group.getChildAt(i);
        if(v instanceof Button){
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    YOUR_ACTION();
                }
            });
        }
 }
Smash answered 29/8, 2016 at 9:27 Comment(1)
To add to this, instead of iterating over the ViewGroup you can just use findViewById. i.e. Button actionButton = (Button) snackbar.getView().findViewById(android.support.design.R.id.snackbar_action);Durmast
W
1

You can try this

final Snackbar snackbar = Snackbar.make("your view".getRootView(), "Annotations", Snackbar.LENGTH_INDEFINITE);
    snackbar.setAction("your action", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    snackbar.show();
                }
            }, 1);
        }
    });
    snackbar.show();

After the action is clicked, snackbar will close automatically but with some delay, so if you call snackbar.show(); directly in the OnClickListener, the snack bar will not show. Therefore, to make it show always, give it some delay before showing it. (surprisingly, a one millisecond delay is enough)

Wharve answered 13/8, 2018 at 14:38 Comment(0)
S
0

You can do the following:

snack = Snackbar.make(binding.root, "Your text", Snackbar.LENGTH_INDEFINITE)
            .setAction("Button Text") {
                // do something here 
                it.postDelayed({
                    snack.show()
                }, 1000) // Snackbar reappears after 1 second
            }
Seldom answered 20/11, 2019 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.