How can I display a Snackbar from a fragment in the correct view?

In my snippet I do:

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 wrong, since Snackbar does not respond to reset-drop gestures and only fills the lower left quadrant in split-view mode. Most Snackbar examples demonstrate calling the Snackbar from an Activity, so I believe that the problem that I am using the snippet is the problem. How can I get and pass the correct view for Snackbar to display correctly?

+5
source share
1 answer

Your problem is not related to your use of fragments.

If your layout does not have a CoordinatorLayout , you will not be able to turn on the enable function, and on tablets the display of the snack will be in the lower left corner of your layout. The spellbook has a maximum width, limiting its filling to the full width of the tablet, however you can also use CoordinatorLayout to center the snack on the tablets, if necessary.

From Android Snackbar.make documentation :

Snackbar will try to find the parent view for viewing Snackbar from the value specified for viewing. The diner will walk through the viewing tree, trying to find a suitable parent, which is defined as a CoordinatorLayout or viewing the contents of window decors, whichever comes first.

Having a Layout coordinator in the hierarchy of your view allows Snackbar to enable certain features, such as automatically turning off and automatically moving widgets, such as FloatingActionButton.

+3
source

All Articles