14

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?

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
apollow
  • 1,250
  • 2
  • 14
  • 22

5 Answers5

36

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.

Imdad
  • 683
  • 1
  • 9
  • 27
apollow
  • 1,250
  • 2
  • 14
  • 22
  • 1
    you need to put other animations too. in_form_top and out_to_top. check this http://stackoverflow.com/a/33333521/1031297 – OWADVL Oct 25 '15 at 18:42
14

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).

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • But I am showing it top of the screen, which means it is now possible. Maybe it was not possible in the past. – zeeshan Jul 24 '17 at 13:44
  • 1
    @zeeshan as per the accepted answer, the animation associated with the Snackbar only assumes a bottom positioning because that is the only positioning that is supported. – ianhanniballake Jul 24 '17 at 13:46
  • Not true. I can put it anywhere. In fact, I just moved it to the center of the screen on the app I am working on, because it looked better there. I think I should post a correct answer and also let Google know to update their documentation. It is common to see outdated, obsolete and misguiding information of Google documentation. – zeeshan Jul 24 '17 at 14:12
  • 1
    Sorry, just because you can do something does not mean it is supported. As mentioned, the animation assumes it is on the bottom of its container. – ianhanniballake Jul 24 '17 at 14:15
  • 1
    I don't understand what you are trying to imply. The question is about positioning of the SnackBar. Anybody can do it, and it is totally supported to put it anywhere on the screen. – zeeshan Jul 24 '17 at 14:20
  • @zeeshan: "it is totally supported to put it anywhere on the screen" -- no, it is not. There are script-kiddie hacks to reposition it (such as with the accepted answer), but the only *Google-supported* location is that the bottom of the screen. Those hacks may not work in future versions of the Design Support Library. If `Snackbar` ever supports custom positioning -- which IMHO is unlikely -- there will be direct methods for controlling the position, the way there is for `Toast`. – CommonsWare Jul 24 '17 at 15:04
  • 1
    That's beyond the scope of this question what Google would support or not, or what it does. I am also not aware of controlling the positioning of Toast. I know that its timing cannot be modified. – zeeshan Jul 24 '17 at 15:37
4

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

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
2

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();
Cheng
  • 775
  • 2
  • 12
  • 28
1

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);
yubaraj poudel
  • 3,821
  • 1
  • 31
  • 28