Controlling the Android AppBar/Toolbar shadow

Today I was fighting the problem of hiding the shadow below the android AppBar or in other cases below the Toolbar. The solution is very simple:

AppBar appBar = (AppBar) findViewById ...
appBar.setEnabled(false;)

Toolbar toolbar = (Toolbar) findViewById ...
toolbar.setEnabled(false;)

Why this works? Well, basically the AppBar uses a stateListAnimator which displays the shadow below it. If we visit the code behind the default AppBar we see that it uses THIS state list animator:

ViewUtilsLollipop.setStateListAnimatorFromAttrs(this, attrs, 0,
                    R.style.Widget_Design_AppBarLayout);

By looking at the default state_list_animator the AppBar uses we see it has:

<item>
        <objectAnimator android:duration="0"
                        android:propertyName="elevation"
                        android:valueTo="0"
                        android:valueType="floatType"/>
    </item>

So this item is used when the AppBarLayout is not enabled. The elevation will be 0dp and no shadow is displayed. Works as a charm!

You may also like...