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.

[MaterialCardView] Background color can't be set to white

See original GitHub issue

Description: I’m trying to set the cardBackgroundColor to #FFF, but it turns to grey. It can’t be set to white unless I added foregroundTint with #FFF too. It works normally with colors other than white.

You can view the screenshot here

Expected behavior: It should be pure white #FFF

Source code:

<com.google.android.material.card.MaterialCardView 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:id="@+id/cardViewVideo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardElevation="0dp"
    app:contentPaddingBottom="4dp"
    app:cardPreventCornerOverlap="true"
    app:cardBackgroundColor="#FFF"
    app:strokeColor="@color/cardview_stroke_color"
    app:strokeWidth="0.5dp">

styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>

Android API version: API version for Preview: 29

Material Library version: 1.1.0

Device: Android Studio 4.0

Note: Changing the app theme to other than Theme.MaterialComponents.Light seems to fix the issue, but I don’t want to change the app theme.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
risalfajarcommented, Jun 12, 2020

UPDATE I looked into material-1.1.0 library values.xml and found out that they set this: <item name="cardForegroundColor">@color/mtrl_card_view_foreground</item>"

Which contains:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
  <item android:alpha="0.08" android:color="?attr/colorPrimary" android:state_checked="true"/>
  <item android:alpha="0.08" android:color="?attr/colorOnSurface" app:state_dragged="true"/>
  <item android:color="@android:color/transparent" android:state_checked="false" app:state_dragged="false"/>
</selector>

I don’t know why in Preview the cardForegroundColor doesn’t show as @android:color/transparent knowing that it is neither checked nor dragged.

My temporary workaround is adding app:cardForegroundColor="@android:color/transparent" to CardView in my layout.

6reactions
gabrielemariotticommented, Aug 25, 2020

It is not a bug, it is related to the Elevation Overlays.

@LoicJ @risalfajar You can check the doc:

Shadows are less effective in an app using a dark theme, because they will have less contrast with the dark background colors and will appear to be less visible. In order to compensate for this, Material surfaces in a dark theme become lighter at higher elevations, when they are closer to the implied light source. This is accomplished via elevation overlays, which are semi-transparent white (colorOnSurface) overlays that are conceptually placed on top of the surface color. The semi-transparent alpha percentage is calculated using an equation based on elevation, which results in higher alpha percentages at higher elevations, and therefore lighter surfaces.

By default the elevationOverlayEnabled (whether the elevation overlay functionality is enabled) is false in Light themes, true in Dark themes.

Just an example with a MaterialCardView with app:cardElevation="4dp" and app:cardElevation="8dp".

Light mode: K0pBS

Dark mode:

FiDKx

If you want to disable it in a dark theme just add in your app theme: <item name="elevationOverlayEnabled">false</item>

Read more comments on GitHub >

github_iconTop Results From Across the Web

CardView background color always white - Stack Overflow
If you want to change the card background color, use: app:cardBackgroundColor="@somecolor". like this: <android.support.v7.widget.
Read more >
Material design MaterialCardView what's going on? Why can't ...
White is #fff or #ffffffff . Also can be set via @android:color/white . Example of usage: app:cardBackgroundColor="#fff".
Read more >
How To Change Background Color Of Cardview Depending If ...
[MaterialCardView] Background color can't be set to white #1402 or try defining a ThemeOverlay overriding this value and apply it to your ...
Read more >
MaterialCardView | Android Developers
Set the stroke color using the strokeColor attribute. Without a strokeColor , the card will not render a stroked border, regardless of the ......
Read more >
Cards - Material Design
android:layout_margin will NOT work in default styles (eg. materialCardViewStyle) so either set this attr directly on a MaterialCardView in the layout or add...
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