How to prevent Snackbar from closing?

2020-03-01 03:08发布

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();

3条回答
虎瘦雄心在
2楼-- · 2020-03-01 03:38

Simply do

Snackbar.make(layout, "Some text", Snackbar.LENGTH_INDEFINITE)
    .show();
查看更多
smile是对你的礼貌
3楼-- · 2020-03-01 03:47

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();
查看更多
干净又极端
4楼-- · 2020-03-01 03:55
snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
查看更多
登录 后发表回答