lundi 20 mai 2019

Snackbar not being dismissed on action click

Through a click event, I'm trying to close a Snackbar whenever it is open. However, for some reason, it doesn't get dismissed, despite opening successfully.

  • If the EditText is empty, I want the Snackbar to appear
  • else if the EditText is not empty, I want the Snackbar to be dismissed

I tried using this, but it didn't work:

if (mSnackbar.isShown) {
    mSnackbar.dismiss()
}

Would a callback need to be used in this situation? If yes, I'm not sure where it needs to go.

val mSnackbar = Snackbar.make(mRootView, "Hello World", Snackbar.LENGTH_LONG)
val mEditText = findViewById(R.id.my_edit_text) as EditText
val mBtn = findViewById(R.id.my_btn) as Button

mBtn.setOnClickListener {
    if (mTextView.text.isEmpty()) {
        mSnackbar.show()
    } else {
        if (mSnackbar.isShown) {
            mSnackbar.dismiss()
        }
    }
} 

Aucun commentaire:

Enregistrer un commentaire