Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change an Android Snackbar's initial alignment from bottom to top?

The recent android library came out just a few days ago, but I would like to have the SnackBar appear on top of the screen, preferably within a RelativeLayout as it's parent view.

How does one change the SnackBar's initial alignment which I presume to be layout_alignParentBottom to layout_alignParentTop?

like image 607
apollow Avatar asked Jun 02 '15 00:06

apollow


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 change the position on my snackbar flutter?

You can do this by placing a container inside the snackbar. Since snackbar can take any widget and you can change its background color to transparent, you can use a container with custom padding and borders to give an illusion of positioning.


2 Answers

It is possible to make the snackbar appear on top of the screen using this:

Snackbar snack = Snackbar.make(parentLayout, str, Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show()

Note: The animation for the snackbar begins from the bottom and surges up to the top of the screen as expected because it was intended to be in the bottom as per ianhanniballake's answer.

For notifications surging from the top, it would probably better off getting a Custom banner instead.

like image 153
apollow Avatar answered Sep 17 '22 19:09

apollow


No, per the material design spec for Snackbars:

Snackbars provide lightweight feedback about an operation by showing a brief message at the bottom of the screen.

As the Design library seeks to implement the material design specs, it is not possible to position the Snackbar in any position other than the bottom of the screen (or containing CoordinatorLayout).

like image 28
ianhanniballake Avatar answered Sep 17 '22 19:09

ianhanniballake