Search code examples
androidkotlinmaterial-designthemescardview

How to override style from material design for every CardView in application?


I use "com.google.android.material:material:1.3.0" in my project. According to Big Nerd Ranch book I try to override styles of CardView for the whole project to avoid setting Style attribute for every CardView. My manifest:

android:theme="@style/Theme.MyProject"

My themes.xml:

<style name="Theme.MyProject" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
    <item name="materialCardViewStyle">@style/MyDesignCardView</item>
</style>

<style name="MyDesignCardView" parent="Widget.MaterialComponents.CardView">
    <item name="cardBackgroundColor">@color/cardViewBackground</item>
    <item name="cardCornerRadius">5dp</item>
    <item name="cardElevation">8dp</item>
</style>

My CardView:

<androidx.cardview.widget.CardView
    android:id="@+id/card_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Solution

  • You should use com.google.android.material.card.MaterialCardView instead of androidx.cardview.widget.CardView