42

Follwing the new android snackbar, i'm trying to set it to be positioned on a specific y coordinate. Its seems to be not even a possible.

I've tried with getting the parent of the snackbar's view, but, there's nothing to be done to the parent for it to set the position of it.

mSnackBar.getView().getParent();

When digging into the actual class, there's an instance of mView, and mParent which is private, and can't be reached anyhow.

https://developer.android.com/reference/android/support/design/widget/Snackbar.html

Dus
  • 4,052
  • 5
  • 30
  • 49

5 Answers5

90

It is possible to set the location that the Snackbar is displayed by positioning a android.support.design.widget.CoordinatorLayout within your existing Activity layout.

For example, say your existing layout is a RelativeLayout you could add a CoordinatorLayout as follows:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/myCoordinatorLayout"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
</android.support.design.widget.CoordinatorLayout>

Then, make sure you pass the CoordinatorLayout as the first argument of the Snackbar.make() command.

final View viewPos = findViewById(R.id.myCoordinatorLayout);    
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
                            .setAction(R.string.snackbar_action_undo, showListener)
                            .show();

This will result in the Snackbar being shown at the bottom of the CoordinatorLayout provided to the make() function.

If you pass a View that is not a CoordinatorLayout the Snackbar will walk up the tree until it finds a CoordinatorLayout or the root of the layout.

BrentM
  • 5,671
  • 3
  • 31
  • 38
  • @BrentM, is there any way to display according to the design guidelines - i.e. centered + floating with some padding at the bottom ? I got it left aligned without padding instead. http://www.google.com/design/spec/components/snackbars-toasts.html – Angel Koh Jul 23 '15 at 13:37
  • @Angel Adding a CoordinatorLayout with `android:paddingBottom="16dp"` and `android:layout_width="match_parent"` should allow you to control the positioning. – BrentM Jul 23 '15 at 21:50
  • 2
    This seems to work perfect. My requirement is to show 2 snackbars in a fragment. So, I added 2 coordinator layouts and passed, them to the respective `Snackbar` objects. However, when the second one appears, the first one goes away. Is there a way to keep both of them visible? – Prerak Sola Jan 24 '17 at 11:37
  • 4
    If you are using Android X, you can use setAnchorView method. https://developer.android.com/reference/com/google/android/material/snackbar/BaseTransientBottomBar.html#setanchorview – qianlv Aug 24 '19 at 08:26
14

Defining a layout explicitly for the Snackbar may not always be practical.

The issue seems to have been targeting the parent, rather than the Snackbar.

Snackbar snackbar = Snackbar.make(layout, message, duration);
View snackbarLayout = snackbar.getView();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT
);
// Layout must match parent layout type
lp.setMargins(50, 0, 0, 0);
// Margins relative to the parent view.
// This would be 50 from the top left.
snackbarLayout.setLayoutParams(lp);
snackbar.show();
Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
6

I have an interesting idea for you.

You can just use the code below to get where you want.

Snackbar.make(View,Message , Snackbar.LENGTH_LONG)
.setAnchorView(pb)
.show();

Here setAnchorView is used to change the postion. You can create your layout in the xml file. Just call your layout inside the setAnchorView and it will show on the position of the xml layout.

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

You can use setAnchorView (@IdRes int anchorViewId) method of a snackbar to set it above anchor view

https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar#make(android.view.View,%20java.lang.CharSequence,%20int)

-1

In addition to brentm's answer, I had to use:

androidx.constraintlayout.widget.ConstraintLayout

and add the dependency for it:

implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"