Trying to make a SnackBar popup menu with buttons. Viewed figured out, but how to make it so that it could not be closed?

    1 answer 1

    Use the Snackbar.LENGTH_INDEFINITE flag when creating the Snackbar . So the snack will not disappear with time and will not move with a finger if there are no CoordinatorLayout among his parents (more precisely, among the parents of the view, which you specify in the first argument of the make method). If it is, then the snack should disable the default behavior in it. To do this, you need to hang up the listener and LayoutParams in it and set the behavior to null - now the snack will not respond to the svayp even in CoordinatorLayout .

     Snackbar snackbar = Snackbar.make(view, "text", Snackbar.LENGTH_INDEFINITE).show(); final View snackbarView = snackbar.getView(); snackbar.show(); snackbarView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { snackbarView.getViewTreeObserver().removeOnPreDrawListener(this); ((CoordinatorLayout.LayoutParams) snackbarView.getLayoutParams()).setBehavior(null); return true; } }); 
    • This flag means that the system will not remove it after some time, but the user can remove it with his finger - RodGers
    • @RodGers, try it like this then: stackoverflow.com/a/41581295/3212712 - Yuri Spb
    • but it worked, thank you, make it accept the answer. It will be good if you describe a little what is happening here - RodGers
    • @RodGers, updated the answer) - Yuriy SPb