androidtoolbaruiviewanimationviewanimatorviewpropertyanimator

Toolbar animation with ViewPropertyAnimator doesn't work second time


First time toolbar animation works good, toolbar animates out of the screen successfully.

Problem: Animation on the toolbar is not working on click of any view, toolbar doesn't reappears in the screen by animation.

xml code of activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_image_viewer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/activity_image_viewer_view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black" />

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

</RelativeLayout>

Toolbar code is:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

</android.support.v7.widget.Toolbar>

Activity code: In onCreate, This will hide the statusbar, navigationbar and toolbar

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
    layoutParams.setMargins(0, uiUtils.getStatusBarHeight(getResources()), 0, 0);
    toolbar.setLayoutParams(layoutParams);

toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
            }
            uiUtils.hideStatusNavigationBar(getWindow());

            ViewTreeObserver observer = toolbar.getViewTreeObserver();
            if (observer.isAlive()) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    observer.removeOnGlobalLayoutListener(this);
                } else {
                    observer.removeGlobalOnLayoutListener(this);
                }
            }
        }
    });

Below is problematic code, Toolbar doesn't reappear by animation In click of any view (OnClickListener is already set on views)

@Override
public void onClick(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
    }
    uiUtils.showStatusNavigationBar(getWindow());
}

Solution

  • By mistake i have set the OnClickListener on ViewPager, Now i have realized that onClick() method never going to be called for view pager. That's why toolbar reappear animation is not working.

    Sometimes we doesn't mind very simple thing.

    Solution: remove OnClickListener from ViewPager and set it to other clickable view.