androidmaterial-designandroid-toolbarandroid-appbarlayoutmaterial-you

Material Design 3 Top app bar does not have a shadow. How to enable it?


The new 'Material Design 3 top app bar' docs says they got rid of the drop shadow. How can I enable it? Setting elevation on Toolbar or AppBar does not work at all. enter image description here


Solution

  • I had the same situation. I found that:

    1. The shadow drop applies starting from API 28, below API 28 - the shadow effect is the same as with a MaterialComponents theme.
    2. A color fill works below API 28 (tested on API 26).

    Docs for Top app bar specs says that the container of the TopAppBar has a role "Surface" and Elevation (on scroll) Level 2.

    On the page Color system - Color roles I found information that:

    At elevation +2 components with surface color containers receive a primary color overlay with 8% opacity.

    So the default style for a TopAppBarLayout uses ?attr/colorSurface as a background color and ?attr/colorPrimary with 8% opacity as an overlay (kind of a scrim effect).

    My solution:

    Create a style for AppBarLayout and set android:outlineAmbientShadowColor and android:outlineSpotShadowColor to black (as it's a default color for creating shadow). These attributes are set as transparent in Widget.Material3.AppBarLayout.

    <style name="Widget.App.AppBarLayout" parent="Widget.Material3.AppBarLayout">
          <item name="android:outlineAmbientShadowColor" ns1:ignore="NewApi">@android:color/black</item>
          <item name="android:outlineSpotShadowColor" ns1:ignore="NewApi">@android:color/black</item>
    </style>
    

    In addition to the above you can add either an android:background attribute with you color or a materialThemeOverlay attribute with setting colorSurface to your color (a background) and colorPrimary to @android:transparent (an overlay). I prefer to add directly android:background because adding materialThemeOverlay can have impact on the child views of your AppBarLayout which.

       <style name="Widget.App.AppBarLayout" parent="Widget.Material3.AppBarLayout">
            <item name="android:outlineAmbientShadowColor" ns1:ignore="NewApi">@android:color/black</item>
            <item name="android:outlineSpotShadowColor" ns1:ignore="NewApi">@android:color/black</item>
            <item name="android:background">@color/white</item>
        </style>
    

    or

    <style name="Widget.App.AppBarLayout" parent="Widget.Material3.AppBarLayout">
            <item name="android:outlineAmbientShadowColor" ns1:ignore="NewApi">@android:color/black</item>
            <item name="android:outlineSpotShadowColor" ns1:ignore="NewApi">@android:color/black</item>
            <item name="materialThemeOverlay">@style/ThemeOverlay.App.DayNight.NoActionBar</item>
        </style>
    
        <style name="ThemeOverlay.App.DayNight.NoActionBar" parent="Theme.Material3.DayNight.NoActionBar">
            <item name="colorPrimary">@android:color/transparent</item>
            <item name="colorSurface">@android:color/white</item>
        </style>
    

    Don't forget apply your style to your AppBarLayout or theme.

    By the way, a liftOnScroll attribute is set to true in Widget.Material3.AppBarLayout so there's no need for setting it. Everything works with setting only layout_behavior for a scrolling view.