SnackBar from top. Is this possible?
Asked Answered
H

4

15

I wanted to provide the SnackBar animation from top, instead of the regular behavior that displays the SnackBar from bottom. Is this easily hackable?

Hilleary answered 27/7, 2015 at 17:59 Comment(0)
S
4

No it is not possible. The documentation states that

They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

You could use a third part library, like Crouton for instance

Silken answered 27/7, 2015 at 18:2 Comment(0)
D
5

it is possible. check this library made by me https://github.com/AndreiD/TSnackBar

basically you add 2 new animations for sliding from top, and change the gravity of the layout. That's all :)

Later Edit: there's a bug going on.... . if anyone wants to spend some time into fixing it we'd all appreciate it :)

Dominate answered 25/10, 2015 at 18:40 Comment(11)
Unfortunately your library suffers from a huge bug, in which the TSnackBar will only appear once if you change activity/fragment, so unfortunately it's unusable in production.Hiedihiemal
@HenriquedeSousa huge..like let's say 100 meters tall ? or huge like 150kg ? .... didn't had time to fix it, but you're welcome to make a PR.Dominate
I'm trying to fix it, but I cannot find the cause, if you know where it is just explain and i'll try to look at it in my spare time, thanks !Hiedihiemal
open source doesn't ask for more.. we're all doing this in our spare time. but if you "need" this library, and you found a solution it would be great to share it with the rest.Dominate
No, I just added a simple view to the top of another, empty layout. No animations, no single support. My client won't pay me to investigate it further :<Hiedihiemal
i have an issue using the provided library every time i change between fragments and i dismiss it the body of snackbar is dismissing first and after split of a second the shadow of snackbar causing a bad user experience..Calx
I know. I need some time to fix that ugly bug. If someone manages to do it before me, please kindly make a push req.Dominate
@Dominate What bug does it have? I did not face any issue while using this lib. But my use case was very small one. I would like to know what issue other people are talking about and possibly fix it if I can. So, can you please tell me what the issue is and how to reproduce that issue?Vaginate
its returning a exception that is "Could not find class 'android.graphics.drawable.VectorDrawable', referenced from method com.androidadvance.topsnackbar.TSnackbar.getBitmap"Floodgate
@HenriquedeSousa I solved that by using a singleton class and create a new snackbar everytime. Works fine for me now. I can share the code if anyone's interested.Mandeville
Yep, you should create your own answer and get the karma upvotes :)Hiedihiemal
S
5
 CoordinatorLayout   coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
  Snackbar snackbar = Snackbar.make(coordinatorLayout, "Text", Snackbar.LENGTH_LONG);
   View view = snackbar.getView();
   CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams)view.getLayoutParams();
                params.gravity = Gravity.TOP;
                view.setLayoutParams(params);
      snackbar.show();
Saponify answered 21/4, 2016 at 11:28 Comment(3)
I have been trying to get this to work for over 48 straight hours. I know it is discouraged to do this, but as someone exhausted from trying everything I found before, thank you, thank you very much. Your solution was the only one that worked for me.Navy
It can move the view to top but animation is still from bottom to up, right?Mitosis
it crashes on android 6Farm
S
4

No it is not possible. The documentation states that

They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

You could use a third part library, like Crouton for instance

Silken answered 27/7, 2015 at 18:2 Comment(0)
D
0

EDIT: This solution renders Snackbar on top, but animation is from bottom.

It is possible, at least with Android Material library and a little trick. You can bind snackbar to a view that renders on top position like this:

On activity_main.xml:

<!-- rest of the components here -->

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/top_coordinator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"/>

On MainActivity.kt:

val snackbar = Snackbar.make(
    findViewById(R.id.top_coordinator),
    "Hello World",
    Snackbar.LENGTH_INDEFINITE
)

snackbar.show()
Duodenary answered 13/6, 2019 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.