1

I am using ConstraintLayout in Android and I would like to display a Snackbar message at the top of the screen. I found this question on Stackoverflow: How to show Snackbar at top of the screen

The answer with the most likes is quite bad as it looks horrible because you can see the Snackbar beeing animated to the top:

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

The second answer assumes that you are using a CorrdinatorLayout which I don't do

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

Do you know how I can display the SnackBar in a ConstraintLayout on top in a nice way (meaning that it will just pop up at the top of the screen without being animated to the top)?

VanessaF
  • 515
  • 11
  • 36

1 Answers1

3

You can use the first piece of code and do the following:

Add this line to resolve the animation issue :

snackbar.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_FADE);

If this doesn't works try this:

snackbar.setAnchorView(*mention viewId above whom you want to show SnackBar*)
Arsh
  • 279
  • 2
  • 6
  • Thanks Arsh for your answer. The first line helped to remove the animation. I accepted and upvoted your answer. Maybe one follow-up question regarding Snackbars. Do you know if it's possible to display the Snackbar longer? I already use `Snackbar.LENGTH_LONG` but this is not long enough for me. Any idea? – VanessaF Aug 01 '22 at 08:40
  • 1
    try ```Snackbar.make(parentLayout, str, Snackbar.LENGTH_INDEFINITE).setDuration(*your duration*);``` – Arsh Aug 01 '22 at 10:07
  • I appreciate your further answer Arsh. Great help. – VanessaF Aug 01 '22 at 12:49