I\'m using a Snackbar from the com.android.support:design:22.2.0
library. I\'m using it to undo deletions. To make my life easier, I\'m going to make the UI loo
Currently you can't achieve it.
There isn't a listener called when the snackbar is dimissed.
The easiest way to do that is to temporarily save the record elsewhere (even a local variable), then re-insert it if they happen to hit the undo button.
Thanks to @SergeyMilakov in Kotlin it is:
@SuppressLint("WrongConstant") // Suppress an error when set duration.
private fun showSnackbar() {
val DURATION = 5000
Snackbar.make(view!!, "Remove item?"), DURATION).apply {
setAction("Undo") {
// Your action when a button is clicked.
}
addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
/* override fun onShown(transientBottomBar: Snackbar?) {
super.onShown(transientBottomBar)
Timber.d("*** onShown")
}*/
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
if (event != Snackbar.Callback.DISMISS_EVENT_ACTION) {
// Your action when the Snackbar is closed and the button was not clicked.
}
}
})
view.setBackgroundColor(ContextCompat.getColor(context, R.color.black))
setActionTextColor(ContextCompat.getColor(context, R.color.yellow))
show()
}
}
Note that Snackbar
shows, even if you move to other screens (for instance, back). So, don't forget to check whether you make actions on the right screen.
Also Snackbar
doesn't restore after screen rotation.
Snackbar.addCallback in kotlin
val snackBar = Snackbar
.make(view, "Text Snackbar", Snackbar.LENGTH_LONG)
.addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
override fun onShown(transientBottomBar: Snackbar?) {
super.onShown(transientBottomBar)
}
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
}
})
val snackBarView = snackBar.view
snackBarView.setBackgroundColor(Color.RED)
snackBar.show()
onDismissed is also called when the action Text is clicked for that reason need to put one condition like
event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT
And now new code is look like below.
final Snackbar snackBar = Snackbar.make(findViewById(R.id.root_layout), result, Snackbar.LENGTH_LONG);
snackbar.addCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) {
// Snackbar closed on its own
}
}
@Override
public void onShown(Snackbar snackbar) {
...
}
});
snackBar.show();
To be notified when a snackbar has been shown or dismissed, you can provide a Snackbar.Callback via setCallback(Callback).
I have exactly the same problem as yours. My solution is...
final Snackbar povrati_obrisani_unos = Snackbar.make (coordinatorLayout, "Ponisti brisanje", Snackbar.LENGTH_INDEFINITE)
.addCallback (new Snackbar.Callback (){
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
super.onDismissed (transientBottomBar, event);
if(event==DISMISS_EVENT_SWIPE){//here we detect if snackbar is swipped away and not cliked (which is undo in my case)
Uri uriDelete = Uri.parse (obrisano.getImageuri ());
ContentResolver contentResolver = getContentResolver();
contentResolver.delete (uriDelete, null, null);
Toast.makeText (MainActivity.this,
"Ocitavanje i slika su trajno obrisani", Toast.LENGTH_SHORT).show ();
}
That is all, do not want to copy-paste snackbar.action which is undo. Want to be clear as I possibly can here.
Regards Nenad