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.

DeryckeS
  • 101
  • 1
  • 5
  • 2
    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) – tjollans May 14 '16 at 18:54
  • So do you want to show snake bar until user manually dismiss it. right? – Krupal Shah May 14 '16 at 18:59
  • 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 action – DeryckeS May 14 '16 at 19:01
  • 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. – DeryckeS May 14 '16 at 20:04

3 Answers3

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();
                }
            });
        }
 }
Tom
  • 408
  • 1
  • 3
  • 7
  • 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);` – RocketSpock Dec 02 '16 at 16:45
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)

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
            }
Paul Spiesberger
  • 5,630
  • 1
  • 43
  • 53