How to set AppBarLayout height programmatically in Android v24.0.0 support library?

When updating using the Android v23.4.0 support library to version v24.0.0, setting the height to 0 programmatically, so that the AppBarLayout application stops working:

appBayLayout.setElevation(0); 

It works when adjusting height in XML.

+8
android android-support-library android-support-design
source share
2 answers

Edit

AppBarLayout of v24.0.0 uses the StateListAnimator , which determines the height depending on its state. Therefore, using setElevation will have no effect if StateListAnimator used (which happens by default). Set elevation via XML or programmatically (as for API> = 21):

 StateListAnimator stateListAnimator = new StateListAnimator(); stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0)); appBarLayout.setStateListAnimator(stateListAnimator); 

Old answer

This seems to be a design support library issue. The problem is how the height is set programmatically using setElevation . An installation from XML puts StateListAnimator in the view and does not call setElevation . However, setElevation should work.

There is a workaround here:

 setDefaultAppBarLayoutStateListAnimator(appBarLayout, 0); @SuppressLint("PrivateResource") private static void setDefaultAppBarLayoutStateListAnimator(final View view, final float targetElevation) { final StateListAnimator sla = new StateListAnimator(); // Enabled, collapsible and collapsed == elevated sla.addState(new int[]{android.R.attr.enabled, android.support.design.R.attr.state_collapsible, android.support.design.R.attr.state_collapsed}, ObjectAnimator.ofFloat(view, "elevation", targetElevation)); // Enabled and collapsible, but not collapsed != elevated sla.addState(new int[]{android.R.attr.enabled, android.support.design.R.attr.state_collapsible, -android.support.design.R.attr.state_collapsed}, ObjectAnimator.ofFloat(view, "elevation", 0f)); // Enabled but not collapsible == elevated sla.addState(new int[]{android.R.attr.enabled, -android.support.design.R.attr.state_collapsible}, ObjectAnimator.ofFloat(view, "elevation", targetElevation)); // Default, none elevated state sla.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0)); view.setStateListAnimator(sla); } 

This is taken from what the constructor does by calling the method in the ViewUtilsLollipop class in v24.0.0.

+10
source share

Another possible solution for this is to add android:stateListAnimator="@null" to your AppBarLayout , as shown below.

 <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stateListAnimator="@null" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
+3
source share

All Articles