3

I'm using CollapsingToolbarLayout in my application in Android. My app's minimum requirement API is 9.

I need the collapsed toolbar to be expanded when the user clicks in the collapsed one, just like in latest Gmail Calendar's app. So I set an onClickListener and inside it I do the following:

public void onClick(View v) {
     if(toolbarExpanded) {
         mAppBar.setExpanded(false, true);
     } else {
         mAppBar.setExpanded(true, true);
     }
     toolbarExpanded = !toolbarExpanded;
 }

Which is working quite well but my problem is that the animation that it's running is slow, meaning a bad user experience.

Is there anyway to change the duration or to define a custom animation for this?

Thank you in advance.

Ignacio Ruiz
  • 611
  • 10
  • 21
  • 1
    The workaround is to rollback to `compile 'com.android.support:design:23.1.0'`, I'm afraid. This behavior has been recently introduced by google, read more here: http://stackoverflow.com/questions/33892376/appbarlayout-setexpandedboolean-true-weird-animation-in-support-library-23-1 – Konstantin Loginov Dec 30 '15 at 18:21

2 Answers2

3

Note: this answer is based on android design library v25.0.0.

You can call the private method animateOffsetTo of the AppBarLayout.Behavior of your NestedScrollView with reflection. This method has a velocity parameter that has an impact on the animation duration.

private void expandAppBarLayoutWithVelocity(AppBarLayout.Behavior behavior, CoordinatorLayout coordinatorLayout, AppBarLayout appBarLayout, float velocity) {
    try {
        //With reflection, we can call the private method of Behavior that expands the AppBarLayout with specified velocity
        Method animateOffsetTo = AppBarLayout.Behavior.getClass().getDeclaredMethod("animateOffsetTo", CoordinatorLayout.class, AppBarLayout.class, int.class, float.class);
        animateOffsetTo.setAccessible(true);
        animateOffsetTo.invoke(behavior, coordinatorLayout, appBarLayout, 0, velocity);
    } catch (Exception e) {
        e.printStackTrace();
        //If the reflection fails, we fall back to the public method setExpanded that expands the AppBarLayout with a fixed velocity
        Log.e(TAG, "Failed to get animateOffsetTo method from AppBarLayout.Behavior through reflection. Falling back to setExpanded.");
        appBarLayout.setExpanded(true, true);
    }
}

To get the Behavior, you need to fetch it from the LayoutParams of your AppBarLayout.

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = params.getBehavior();
Raphael Royer-Rivard
  • 2,252
  • 1
  • 30
  • 53
3

To expand with animation use

 AppBarLayout.setExpanded(true,true);

To Collapse with animation use

 AppBarLayout.setExpanded(false,true);
Anbarasu Chinna
  • 975
  • 9
  • 28