How to put a CardView attribute in a style?

AndroidAndroid Cardview

Android Problem Overview


I have:

in a file build.gradle (dependencies)

dependencies {
    compile 'com.android.support:cardview-v7:21.0.+'
}

in a file styles.xml (styles definition)

<resources
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">
    
    <style name="CardViewStyle">
        <item name="android:layout_margin">5dip</item>
        <item name="card_view:cardCornerRadius">4dp</item>
        <item name="card_view:cardElevation">4dp</item>
    </style>
</resources>

But compiler complains:

Error: No resource found that matches the given name: attr 'card_view:cardCornerRadius'.

How do I add some cardView attributes in a style?

Android Solutions


Solution 1 - Android

Set parent attribute to CardView.
You don't even have to add xmlns:card_view="http://schemas.android.com/apk/res-auto".

Working snippet of code:

<style name="CardViewStyle" parent="CardView">
     <item name="cardCornerRadius">4dp</item>
     <item name="cardElevation">4dp</item>
</style>

Solution 2 - Android

To add to mklimek's answer, you do not even need to add the parent="CardView"

Just avoid the cardView: prefix

So instead of

    <item name="cardView:cardElevation">5dp</item>

just use

    <item name="cardElevation">5dp</item>

So my complete style code for this is:

<style name="categoriesCardViewButtons">
    <item name="android:layout_width">80dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">10dp</item>
    <item name="cardCornerRadius">8dp</item>
    <item name="cardElevation">5dp</item>
    <item name="cardBackgroundColor">@color/grey_ultra_light</item>
    <item name="android:paddingBottom">@dimen/l1LayoutPaddingB</item>
</style>

Solution 3 - Android

This error can occur if you do not have CardView in your gradle dependencies (in Android Studio) for the module containing your style (even though you have the dependency in the module where you use the card view). In that case, simply add

compile 'com.android.support:cardview-v7:22.2.1'

(or another version) to build.gradle for the module containing themes.xml or styles.xml.

Then, you do not need to set a parent on your style, nor do you need a namespace prefix, but can follow the example in tsik's answer, so all you need is

<style name="myStyle">
    <item name="cardCornerRadius">8dp</item>
</style>

Solution 4 - Android

This code work well

<style name="MyCardView" parent="CardView">
    <item name="cardUseCompatPadding">true</item>
    <item name="cardCornerRadius">@dimen/hei_standard_8dp</item>
    <item name="cardBackgroundColor">@color/bg_item_list_alphabet</item>
</style>

Note: parent ="CardView"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexander KulyakhtinView Question on Stackoverflow
Solution 1 - AndroidklimatView Answer on Stackoverflow
Solution 2 - AndroidtsikView Answer on Stackoverflow
Solution 3 - AndroidssenatorView Answer on Stackoverflow
Solution 4 - AndroidHai RomView Answer on Stackoverflow