[MaterialCardView] Background color can't be set to white
See original GitHub issueDescription:
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:
- Created 3 years ago
- Reactions:12
- Comments:13 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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.It is not a bug, it is related to the Elevation Overlays.
@LoicJ @risalfajar You can check the doc:
By default the
elevationOverlayEnabled
(whether the elevation overlay functionality is enabled) isfalse
in Light themes,true
in Dark themes.Just an example with a
MaterialCardView
withapp:cardElevation="4dp"
andapp:cardElevation="8dp"
.Light mode:
Dark mode:
If you want to disable it in a dark theme just add in your app theme:
<item name="elevationOverlayEnabled">false</item>