androidandroid-elevation

Elevation shadow not visible for Toolbar with Basic Activity


I created a "Basic Activity" with Android Studio and attempted to create an elevation shadow on an API 28 Pixel device, using the answer from toolbar-not-showing-elevation-in-android-9-api-28. However, there is no elevation shadow displayed. The activity_main.xml file currently is:

<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

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

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

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

    <include layout="@layout/content_main"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Why does the elevation shadow no longer work on Toolbar?


Solution

  • 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