Search code examples
androidandroid-actionbarandroid-animationandroid-toolbarandroid-fullscreen

Android - Show/Hide Toolbar in app on user touch with slide up and slide down animation


I have implemented Show/Hide Toolbar when the user scrolls the list using CoordinatorLayout. And now I am working Show/Hide Toolbar in android app on user touch anywhere on the screen. I have tried this code and it is working fine:

        if (toolbar.getVisibility() == View.VISIBLE) {
            toolbar.setVisibility(View.GONE);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            toolbar.setVisibility(View.VISIBLE);
        }

But the only problem is that toolbar does not animate while hiding or showing. I want that toolbar should slide up and slide down while hiding and showing.


Solution

  • if (toolbar.getVisibility() == View.VISIBLE) {
        appbar.animate().translationY(-112).setDuration(600L)
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        toolbar.setVisibility(View.GONE);
                    }
                }).start();
    } else {
        toolbar.setVisibility(View.VISIBLE);
        appbar.animate().translationY(0).setDuration(600L).start();
    }