32

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();
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • is it part of the framework ? – Blackbelt Apr 28 '15 at 14:00
  • Looks so..... Its cool ... I am using a backward compatibility library (https://github.com/navasmdc/MaterialDesignLibrary) check it ... its like a crouton notification – Devrath Apr 28 '15 at 14:01
  • no it is not. It is a third part library – Blackbelt Apr 28 '15 at 14:03
  • @ Blackbelt .... I was looking at the google docs .... found this(https://developer.android.com/reference/android/support/design/widget/Snackbar.html) looks like its part of android library – Devrath Jun 20 '15 at 04:53

3 Answers3

49

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();
Community
  • 1
  • 1
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Third party library .... ? ... ok ...i was not aware of that. check thhe edit ... i cannot resolve this show() – Devrath Apr 28 '15 at 14:18
  • `setIndeterminate` returns void not `SnackBar`. Assign the reference and call .setIndeterminate(true) before calling show() – Blackbelt Apr 28 '15 at 14:22
  • 1
    @Devrath I ve update my answer. Let me know if you need help the android's version of snackbar. To show it indefinitely you can use `LENGTH_INDEFINITE` as duration – Blackbelt Aug 26 '15 at 08:13
  • @Blackbelt , using either "-2" or `Snackbar.LENGTH_INDEFINITE` throws errors in Android Studio ... – Philip Kahn Aug 28 '15 at 05:59
  • 2
    @PhilipKahn you need the latest support design library to use `Snackbar.LENGTH_INDEFINITE`. (22.2.1) – Blackbelt Aug 28 '15 at 09:06
15
snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
Nazik
  • 8,696
  • 27
  • 77
  • 123
5

Simply do

Snackbar.make(layout, "Some text", Snackbar.LENGTH_INDEFINITE)
    .show();
Vlad
  • 7,997
  • 3
  • 56
  • 43