Fábio Carballo
Fábio Carballo

Reputation: 3285

SnackBar from top. Is this possible?

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

Upvotes: 15

Views: 25741

Answers (4)

Codigo Morsa
Codigo Morsa

Reputation: 840

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

Upvotes: 0

Vijayan R
Vijayan R

Reputation: 619

 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();

Upvotes: 5

OWADVL
OWADVL

Reputation: 11154

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

Upvotes: 5

Blackbelt
Blackbelt

Reputation: 157487

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

Upvotes: 4

Related Questions