In my fragment, I perform:
mActionHelper.showUndoBar(getView(), itemsList, lastPositionSelected);
The showUndoBar() method simply creates a Snackbar with the form:
Snackbar.make(view, message, Snackbar.LENGTH_LONG);
However, somehow the view is incorrect, as the Snackbar does not respond to swipe-to-dismiss gestures and only fills up the bottom-left quadrant in tablet split-view. Most of the Snackbar examples demonstrate calling the Snackbar from an Activity, so I think the fact that I am using a Fragment is the problem. How can I obtain and pass the correct View for the Snackbar to display correctly?
Your issue is not related to your use of Fragments.
If you don't have a CoordinatorLayout
in your layout you will not have swipe to dismiss functionality enabled and on tablets the display of the Snackbar will be at the very bottom left of your layout. The Snackbar has a maximum width stopping it filling the complete width of a tablet, however you can also use the CoordinatorLayout
to center the Snackbar on tablets if desired.
From the Android Snackbar.make documentation:
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets like FloatingActionButton.