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
?
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.
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
).
Here's a simple way to add SnackBar using CoordinatorLayout from top of the screen but animation is still an issue.
Simply add a CoordinatorLayout layout to your layout like:
<android.support.design.widget.CoordinatorLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/snackbarlocation">
</android.support.design.widget.CoordinatorLayout>
and then in your Activity get a reference to it and pass it to the SnackBar as below:
CoordinatorLayout Clayout = (CoordinatorLayout)findViewById(R.id.snackbarlocation);
FabButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(Clayout, "This snack bar located at top", Snackbar.LENGTH_SHORT).show();
}
});
Credits
It can be done in simple ways
first Create the layout where snackbar to be included
<LinearLayout
android:id="@+id/info_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
then in Code
ll_mesg.addView(Snackbar.make(ll_mesg, "Please choose your luck from one of box", Snackbar.LENGTH_LONG)
.getView());
new Handler().postDelayed(new Runnable() {
public void run() {
ll_mesg.removeAllViews();
}
}
,1000);
Well, you can get snackbar's layoutparam and change the bottomMargin in the code.
like
Snackbar snackbar = Snackbar.make(rootView, message, Snackbar.LENGTH_LONG);
View view = snackbar.getView();
FrameLayout.LayoutParams para = (CoordinatorLayout) view.getLayoutParams();
params.bottomMargin = showAtLocation // pass your location offset here
view.setLayoutParams(params);
Then you can use snackbar instance to show whenever you want
snackbar.show();