0

I made a snackbar on the top of my layout. BUT, when it appears, it slides upwards and when it is dismissed, it slides downwards. My wish is to make it appear from the top of the screen and slides down, and vice-versa.

CoordinatorLayout coordinatorLayout = findViewById(R.id.v_set_bgs);
snackbar = Snackbar.make(coordinatorLayout, R.string.changes, Snackbar.LENGTH_INDEFINITE);
View view = snackbar.getView();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
params.gravity = Gravity.START;
TextView snackbarText = view.findViewById(android.support.design.R.id.snackbar_text);
snackbarText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
view.setLayoutParams(params);
view.setAlpha(0.75f);
if (snackbarShow && isAlready == 0 && AppSettings.isChanged) {
    isAlready = 1;
    snackbar.show();
}

So. So far, it's working like a normal snackbar (on the top of the layout). I just couldn't find any method how to change the slide direction.

Mihai Coman
  • 73
  • 1
  • 9

2 Answers2

1

According to the documentation

Snackbars provide lightweight feedback about an operation. 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.

The best option you can do is extend Snackbar class and override the show method or another whatever is responsible for the animation

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
1

This makes the Snackbar appears on Top without the strange slide in transition.

Best simple solution in Kotlin:

val snackbar = Snackbar.make(view, string, Snackbar.LENGTH_LONG)
val layoutParams = LayoutParams(snackbar.view.layoutParams)

layoutParams.gravity = Gravity.TOP
snackbar.view.setPadding(0, 10, 0, 0)
snackbar.view.layoutParams = layoutParams
snackbar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
snackbar.show()