Search code examples
androiddismissfloating-action-buttonsnackbar

FloatingActionButton does not come down when dismissing Snackbar


I am trying to use a Snackbar. I have a FloatingActionButton wrapped in a CoordinatorLayout. When the Snackbar shows, the button is correctly moved up. When it dismisses automatically, the button moves down. But if I dismiss the Snackbar programmatically, the button does not go down. My code is simple:

mSnackbar = Snackbar.make(mCoordinatorLayout, text, Snackbar.LENGTH_LONG)
                .setAction(R.string.undo, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        undoDeleteTasks();
                    }
                });
        mSnackbar.show();

Is there a way to make the FloatingActionButton move down when the Snackbar is dismissed programmatically?


Solution

  • Try this:

    Snackbar mysnack = Snackbar.make( fab, "Hi, welcome to my app!", Snackbar.LENGTH_LONG );
    mysnack.getView().addOnAttachStateChangeListener( new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow( View v ) {
    
        }
    
        @Override
        public void onViewDetachedFromWindow( View v ) {
            fab.setTranslationY( 0 );
        }
    });
    mysnack.show();