Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 240
Steve M Avatar asked Jul 17 '19 00:07

Steve M


People also ask

Which method sets the Toolbar as the app bar for the activity?

To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar.

How to enable Action bar in Android Studio?

setDisplayUseLogoEnabled(true); getSupportActionBar(). setLogo(R. drawable. ic__actionbar); // Set up the action bar.

What is default elevation for toolbar?

The default elevation of the action bar is 4dp.

How do I get rid of the shadow bar on my android?

By default, android provides shadow for action bar. This example demonstrates How to remove shadow below the action bar. Step 1 - Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 - Add the following code to res/layout/activity_main.


1 Answers

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

like image 70
Cheticamp Avatar answered Sep 24 '22 23:09

Cheticamp