Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollapsingToolbarLayout expand programmatically animation duration

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.

like image 706
Ignacio Ruiz Avatar asked Oct 29 '25 17:10

Ignacio Ruiz


2 Answers

To expand with animation use

 AppBarLayout.setExpanded(true,true);

To Collapse with animation use

 AppBarLayout.setExpanded(false,true);
like image 124
Anbarasu Chinna Avatar answered Nov 01 '25 09:11

Anbarasu Chinna


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();
like image 27
Raphael Royer-Rivard Avatar answered Nov 01 '25 09:11

Raphael Royer-Rivard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!