Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you adjust Android SnackBar to a specific position on screen

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

like image 605
Dus Avatar asked Jul 18 '15 14:07

Dus


People also ask

How do you set a snackbar position?

Snackbars should be placed at the bottom of a UI, in front of app content.

How do I adjust my snackbar height?

setTextSize(24); //increase max lines of text in snackbar. default is 2. sbNoAct. show(); int height = sbView.


1 Answers

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.

like image 131
BrentM Avatar answered Sep 26 '22 00:09

BrentM