javaandroidanimationviewpropertyanimator

ViewPropertyAnimator - animation wouldn't happen the first time


I'm using the following code for a slide in / slide out toggle for an AppBarLayout.

public void showToolbar(boolean show) {
    if (appBar == null) {
        Log.e(TAG, "showToolbar: Toolbar is null");
        return;
    }
    boolean toolbarShown = Utils.isViewVisible(appBar);
    Log.d(TAG, "showToolbar: shown:" +shown);
    boolean changed = (show != toolbarShown);
    if (changed) {
        if (show) {
            Log.d(TAG, "showToolbar: showing");
            appBar.setVisibility(View.VISIBLE);
            appBar.animate()
                    .translationY(0)
                    .setInterpolator(new DecelerateInterpolator())
                    .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) {
                            appBar.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationEnd(Animator animator) { }

                        @Override
                        public void onAnimationCancel(Animator animator) { }

                        @Override
                        public void onAnimationRepeat(Animator animator) { }
                    })
                    .start();
        } else {
            Log.d(TAG, "showToolbar: hiding");
            appBar.animate()
                    .translationY(-toolbar.getBottom())
                    .setInterpolator(new DecelerateInterpolator())
                    .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) { }

                        @Override
                        public void onAnimationEnd(Animator animator) {
                            appBar.setVisibility(View.INVISIBLE);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) { }

                        @Override
                        public void onAnimationRepeat(Animator animator) { }
                    })
                    .start();
        }
    } else {
        Log.d(TAG, "showToolbar: no change");
    }
}

The animation works perfectly except for the first time showToolbar(true) is called to show the toolbar. The view just shows up without animation the first time. I have scoured the site and found similar questions but the solutions doesn't seem to be working for me.

It might be worth noting that this happens only when we want the appBar to be hidden first. My guess is that maybe for the animation to

Update 1:

public static boolean isViewVisible(View view) {
    if (View.VISIBLE == view.getVisibility()) return true;
    else return false;
}

Update 2

I have removed isViewWithinScreenBounds() method because that check isn't really needed.


Solution

  • Be sure to set the initial values for visibility and translationY.

    If you want your toolbar to be initially hidden and show up with the first animation, be sure to set android:visibility="invisible" and NOT "gone", and negative android:translationY like -56dp.