Amando Vledder
Amando Vledder

Reputation: 1346

AppBarLayout with a Toolbar has no shadow/ elevation

I am trying to add a toolbar to my activity using a AppBarLayout but i am unable to get any elevation or shadow on it.

I am using the following layout and styles;

<!-- activity -->
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        style="@style/Widget.App.AppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"  />

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

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main_graph"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
<!-- styles -->
<style name="App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorSecondary">@color/colorAccent</item>
    <item name="colorSurface">@color/background</item>

    <item name="toolbarStyle">@style/Widget.App.Toolbar</item>
    <item name="android:statusBarColor">@color/background</item>
    <item name="android:windowLightStatusBar">true</item>
    <item name="android:navigationBarColor">@android:color/white</item>
    <item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
</style>

<style name="App.PopupTheme" parent="ThemeOverlay.MaterialComponents.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorSecondary">@color/colorAccent</item>
</style>

<style name="Widget.App" parent="@android:style/Widget.Material" />

<style name="Widget.App.AppBar" parent="@style/Widget.Design.AppBarLayout">
    <item name="android:background">@color/white</item>
    <item name="android:elevation">12dp</item>
    <item name="elevation">12dp</item>
</style>

<style name="Widget.App.Toolbar" parent="Widget.MaterialComponents.Toolbar">
    <item name="android:paddingEnd">8dp</item>
    <item name="contentInsetStartWithNavigation">64dp</item>
    <item name="popupTheme">@style/App.PopupTheme</item>
</style>

Even without any styling at all the toolbar is shown without any shadow to it.

Using the code above i get

enter image description here

But it should look like this

enter image description here

I have already tried copying styles from the open source Google IO app (which the second screenshot is from) but that didn't work either.

I am using

'com.google.android.material:material:1.2.0-alpha03' (also tried the latest stable version)

Thanks in advance

Upvotes: 4

Views: 1409

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363469

You can add android:clipChildren="false" to the parent view.

<androidx.coordinatorlayout.widget.CoordinatorLayout 
    android:clipChildren="false">

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

        <androidx.appcompat.widget.Toolbar
           app:elevation="12dp"
        />

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

Also the appcompat library by default applies an alpha channel to the shadow only for api28+. You can change this behavior adding in your app theme the android:spotShadowAlpha attribute (only for api28+):

<!--Alpha value of the spot shadow projected by elevated views, between 0 and 1.-->
<item name="android:spotShadowAlpha">0.x</item>

Upvotes: 1

Related Questions