Elevation shadow not visible for Toolbar with Basic Activity

With the XML that you provided, add android:clipChildren="false" to the CoordinatorLayout. This will permit the shadow to be drawn outside the bounds of the AppBarLayout.

The XML now looks like this (simplified here):

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clipChildren="false">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="16dp"
            app:title="The Toolbar"
            app:titleTextColor="@android:color/white" />

    </com.google.android.material.appbar.AppBarLayout>

This is really going to be enough for the shadow to be displayed. The problem that I had here was that the shadow was so faint on API 28 and 29 that I could not distinguish it from the toolbar.

To make the shadow more visible, create a styles file for v28 and add the following to the application's theme:

<item name="android:spotShadowAlpha">0.5</item>

It seems that the default alpha value is too faint to be readily seen. This value corrects the problem.

"Playing with elevation in Android (part 1)" does a good job of explaining this issue.

The layout now looks like this on an API 29 emulator:

enter image description here


You can set the elevation for AppBarLayout to achieve the shadow.
Example (in example, I set marginEnd and color white for Toolbar just for see the shadow easily)

<com.google.android.material.appbar.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:elevation="16dp"
    android:layout_marginEnd="50dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#fff"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</com.google.android.material.appbar.AppBarLayout>

I tested on Pixel emulator (android 9), Huawei device (android 8) and it work (shadow display).

I think if the parent ViewGroup have width height equal to children, it will not show the shadow of children. I try your case with different ViewGroup, like ContrainstLayout, RelativeLayout and get same behavior.