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 ...

Toolbar toolbar = (Toolbar) findViewById ...

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,

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

        <objectAnimator android:duration="0"

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...