1

I would like to create an unswipe-able Snackbar in an activity which contains a DrawerLayout.

Either I use a FrameLayout to contain my Snackbar and it appears on top of the NavigationView when I draw it, or I use a CoordinatorLayout and it appears under, but it becomes swipe-able.

In Google's Component documentation, it is said in the usage that the Snackbar is lower in elevation than the DrawerLayout.

I tried, in vain, to set a dummy behavior to my CoordinatorLayout to prevent it from being swipe-able, like discussed in this previous post.

Therefore, how come a Snackbar wrapped in a FrameLayout within a DrawerLayout appears above the DrawerLayout? and is there a way to override the dismiss behavior of a Snackbar in a CoordinatorLayout?

1 Answers1

0

I found out a solution to override the default dismiss behavior set to the Snackbar:

View rootView = findViewById(coordinatorLayoutId);

//Create Snackbar
Snackbar snackbar = Snackbar
            .make(rootView, notificationTextId, Snackbar.LENGTH_INDEFINITE)
            .setAction(actionTextId, clickListener)
            .setCallback(new Snackbar.Callback() {
                @Override
                public void onShown(Snackbar snackbar) {
                    ViewGroup.LayoutParams rootViewGroupParams = view.getLayoutParams();

                    if (rootViewGroupParams instanceof CoordinatorLayout.LayoutParams) {
                        final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) rootViewGroupParams;
                        params.setBehavior(null);
                        view.setLayoutParams(params);
                }
            });

Therefore, using the CoordinatorLayout as a root view will get the Snackbar under the DrawerLayout, and redefining the onShown() Method in the Snackbar.Callback will allow one to redefine the CoordinatorLayout behavior after it was shown, thus preventing it from being swiped.