No shadow by default on Toolbar?
I ended up setting my own drop shadow for the toolbar, thought it might helpful for anyone looking for it:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_alizarin"
android:titleTextAppearance="@color/White"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- **** Place Your Content Here **** -->
<View android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@drawable/toolbar_dropshadow"/>
</FrameLayout>
</LinearLayout>
@drawable/toolbar_dropshadow:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="@android:color/transparent"
android:endColor="#88333333"
android:angle="90"/>
</shape>
@color/color_alizarin
<color name="color_alizarin">#e74c3c</color>
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:
Toolbar shadow not showing
I got it to work by adding the following codes to my MainActivity class:
AppBarLayout layout = (AppBarLayout) findViewById(R.id.appbar);
layout.setStateListAnimator(null);
ViewCompat.setElevation(layout, 8);
Elevation on Material Toolbar doesn't produce a shadow
You have to 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="8dp"
/>
</com.google.android.material.appbar.AppBarLayout>
Also by default an alpha channel is applied to the shadow for api28+.
You can change this value 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>
Can not change shadow on Toolbar with AppBar
Change your AppBarLayout
code to:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stateListAnimator="@animator/appbar_always_elevated"
android:theme="@style/AppTheme.AppBarOverlay">
And add appbar_always_elevated.xml
in animator
directory with this content:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<objectAnimator android:propertyName="elevation"
android:valueTo="1dp"
android:valueType="floatType"
android:duration="1"/>
</item>
</selector>
It should work now.
You can change android:valueTo
value to set shadow. Also, app:elevation
is not working on new APIs.
@deprecated target elevation is now deprecated.
Source: https://chromium.googlesource.com/android_tools/+/refs/heads/master/sdk/sources/android-25/android/support/design/widget/AppBarLayout.java?autodive=0%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F
Material Design - AppCompat toolbar without showing shadow
Those two are completely different shadows. The vertical one is that of DrawerLayout
. It's supposed to be showing beside expanded drawer. The horizontal one is part of windowContentOverlay
on APIs below LOLLIPOP
(on LOLLIPOP
it's @null
).
When you work with Toolbar
widget the toolbar isn't part of window decor anymore so the shadow starts at the top of the window over the toolbar instead of below it (so you want the windowContentOverlay
to be @null
). Additionally you need to add an extra empty View
below the toolbar pre-LOLLIPOP
with its background set to a vertical shadow drawable (8dp tall gradient from #20000000
to #00000000
works best). On LOLLIPOP
you can set 8dp elevation on the toolbar instead.
Note: Use the same gradient but horizontal as the drawer shadow for best results.
How to remove the shadow under Action bar
Set this property toolbar:elevation="0dp"
to AppBarLayout
to remove shadow from Toolbar
.
eg-
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
toolbar:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
Remove android.widget.Toolbar shadow
I have found solution myself:
getActionBar().setElevation(0);
More info:
https://developer.android.com/reference/android/app/Activity.html#getActionBar()
Related Topics
Executing Google Apps Script Functions from Mobile App
Android Gridlayout with Dynamic Number of Columns Per Row
How to Create and Read Log on Android Devices
No Changes to Deploy Android Studio 2.1
Datepicker Dialog Without Calendar Visualization in Lollipop [Spinner Mode]
How to Cancel This Repeating Alarm
Retrieving All Drawable Resources from Resources Object
Add Extra User Information with Firebase
Service Won't Stop When Stopservice Method Is Called
How to Import Material Design Library to Android Studio
How to Start Fragment from an Activity
Spannablestring with Image Example
Android Drawable Images from Url
What Is the Relation Between App_Platform, Android:Minsdkversion and Android:Targetsdkversion