Search code examples
androidandroid-5.0-lollipopmaterial-design

How to prevent Snackbar from closing?


I'm using a control, called Snackbar, from a 3rd party library - github.com/navasmdc/MaterialDesignLibrary.

The problem is that it is closing automatically, like a Toast.

What i am trying to do: It should stay until i click btn

Code snippet i am using

new SnackBar(ActSplash.this,
      "Do you want change color of this button to red?",
      "yes", 
       new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ButtonFlat btn = (ButtonFlat) findViewById(R.id.buttonSnackBar);
             //btn.setTextColor(Color.RED);
        }
}).show();

EDIT:

new SnackBar(ActSplash.this,
                                "Do you want change color of this button to red?",
                                "yes", new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                ButtonFlat btn = (ButtonFlat) findViewById(R.id.buttonSnackBar);
                                //btn.setTextColor(Color.RED);
                            }
                        }).setIndeterminate(true)
                                .show();

This shows a error cannot resolve show()


EDIT- FROM - NEW ANDROID DOCS

How to prevent android snackbar from closing

Snackbar
                 .make(((ActMedicalRecordDetailNew)getActivity()).getMainContent(), R.string.snackBarNoNetConnectivity, Snackbar.LENGTH_LONG)
                    .setAction(R.string.snackBarTryAgain, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = getActivity().getIntent();
                            getActivity().finish();
                            startActivity(intent);
                        }
                    })
                    .show();

Solution

  • Edit:

    Snackbar is now part of the new Android Support Design library. you can use LENGTH_INDEFINITE as duration if you want to show it indefinitely. . You should drop the third party library you are using for it. Eg.

    Snackbar.make(layout, R.string.snackBarNoNetConnectivity, Snackbar.LENGTH_INDEFINITE)
        .show()
    

    Old answer

    you have to call .setIndeterminate(true) before calling show()

    final SnackBar tmp = new SnackBar(ActSplash.this,
          "Do you want change color of this button to red?",
          "yes", 
           new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  ButtonFlat btn = (ButtonFlat) findViewById(R.id.buttonSnackBar);
                 //btn.setTextColor(Color.RED);
            }
    });
    tmp.setIndeterminate(true);
    tmp.show();