Elevation shadow not visible for Toolbar with Basic Activity
Asked Answered
H

3

6

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?

Hermitage answered 17/7, 2019 at 0:5 Comment(1)
Can you post a simple project with that setup at github?Gunmaker
G
15

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

Georgeta answered 24/7, 2019 at 16:25 Comment(0)
P
2

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.

Punchdrunk answered 24/7, 2019 at 9:21 Comment(0)
B
-1
<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:elevation="4dp">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:buttonGravity="center_vertical"
        app:titleView="@layout/toolbar_sub_category_title"
        tools:ignore="Overdraw" />
</androidx.cardview.widget.CardView>
Britnibrito answered 16/3, 2022 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.