question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MaterialToolbar] Elevation resets to 0dp in Dark Theme

See original GitHub issue

Description: When using MaterialToolbar with the Theme.MaterialComponents.DayNight.NoActionBar theme, the toolbar’s elevation resets to 0dp during the Night mode. As a result, the background color of toolbar (which it gets from ?attr/colorSurface) is the same as the activity’s background color. So, we cannot differentiate between the toolbar and the activity’s background. This happens because at 0dp elevation, the Dark Theme sets the colorSurface to the same value as the colorBackground.

The expected behavior should be that the Toolbar’s elevation is still 4dp in the Night mode as well.

Please reply if anybody else is having this same problem, or am I the only victim. 😅

Expected behavior:

Material Dark theme

image

Source code: Here’s the link to file.

As you can see, in the following style, the android:elevation is not set to the Toolbar. And, this style gets applied in Night mode.

<style name="Widget.MaterialComponents.Toolbar.Surface">
    <!-- This line is missing from the style -->
    <item name="android:elevation" tools:ignore="NewApi">@dimen/design_appbar_elevation</item>

    <item name="android:background">?attr/colorSurface</item>
    <item name="titleTextColor">@color/material_on_surface_emphasis_high_type</item>
    <item name="subtitleTextColor">@color/material_on_surface_emphasis_medium</item>
    <!-- Note: this theme overlay will only work if the style is applied directly to a Toolbar. -->
    <item name="android:theme">@style/ThemeOverlay.MaterialComponents.Toolbar.Surface</item>
</style>

Android API version: Android 11 (API 30)

Material Library version: 1.5.0

Device: Samsung Galaxy A50s

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dsn5ftcommented, Mar 8, 2022

Technically Widget.MaterialComponents.Toolbar is the default style, not Widget.MaterialComponents.Toolbar.Primary. The reason why the Widget.MaterialComponents.Toolbar.Surface doesn’t have the elevation is because Surface Toolbars are meant to be flush with the rest of the screen, without a shadow (until scroll which is what the AppBarLayout is for).

1reaction
dsn5ftcommented, Mar 4, 2022

Hi @KdB13, thanks for filing your issue. By default the Toolbar styles are meant to not come with an elevation, because a common use case and recommendation is to embed a Toolbar within an AppBarLayout, which will provide the elevation and the background color, as well as more Material behavior like hide-on-scroll or lift-on-scroll. So setting the elevation manually like you found is actually correct, if using a standalone Toolbar.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Elevation on Material Toolbar doesn't produce a shadow
1 Answer 1. Sorted by: Reset to default. Highest score (default) ...
Read more >
Dark Theme: Elevation Making No Difference in View ... - GitHub
While trying out the Dark Theme sample I tried to manipulate a CardView's perceived elevation by changing the elevation value in xml.
Read more >
App bars: top - Material Design
The MaterialToolbar can be set as the support action bar and thus receive ... Primary (light theme) or surface (dark theme) background color...
Read more >
MaterialToolbar - Android Developers
MaterialToolbar is a Toolbar that implements certain Material features, such as elevation overlays for Dark Themes and centered titles.
Read more >
Using the App Toolbar | CodePath Android Cliffnotes
The easiest way is to have your theme extend from Theme. ... parent layout of the Toolbar to ensure that the height of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found