7

Is there a way to apply a theme to a Cardview? I don't want to apply the style to every single view i'm creating.

This is my CardView right now:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Custom.Widget.CardView">
    <-- I don't want to define the style every time -->
</android.support.v7.widget.CardView>

And my styles.xml

<style name="Custom.Widget.CardView" parent="CardView">
    <item name="cardBackgroundColor">@color/card_backgroundColor</item>
    <item name="cardCornerRadius">12dp</item>
    <item name="cardUseCompatPadding">true</item>
    <item name="contentPadding">4dp</item>
</style>

I would like to add the style to themes.xml so that it will be applied to every Cardview, but I don't know how. Is it even possible for views from the support library?

When I add the following to themes.xml I get a warning: no resource found that matches the given name: attr 'Cardview'

<item name="Cardview">@style/Custom.Widget.CardView</item>
Wirling
  • 4,810
  • 3
  • 48
  • 78
  • Does [this SO question](http://stackoverflow.com/questions/30417847/attach-custom-cardview-style-to-theme) answer your question too? – Sufian Oct 01 '16 at 10:11
  • No that only solves the problem of applying a different style based on the light or the dark theme. However I found a similar [question](http://stackoverflow.com/questions/29914671/how-to-add-cardview-attributes-to-app-theme) with a solution to write some sort of a wrapper class. That would probably work but I don't think it's the most elegant solution because I would have to replace the support library class with my own implementation. – Wirling Oct 03 '16 at 11:24

2 Answers2

7

It's possible, I also took a long time to find. Here is an example that applies an orange background to all cardview if the MyStyle style is used throughout the app.

In themes.xml:

<style name="MyStyle" parent="Theme.AppCompat">
    ...

    <item name="cardViewStyle">@style/MyStyle.Cardview.Orange</item>
</style>

<!-- CardView -->
<style name="MyStyle.Cardview.Orange" parent="CardView">
    <item name="cardBackgroundColor">#F3921F</item>
</style>
J-Jamet
  • 847
  • 8
  • 17
  • Is there a big list with all the style names so that we can find out how each element is called ? – Hydrocat Aug 17 '20 at 13:24
  • You have to look in the source code and test. I did several tests before I found the right elements. The doc is not great at this level. – J-Jamet Aug 20 '20 at 08:23
0

I also use this in my project and no problem,May be you not add this in your build.gradle

compile 'com.android.support:cardview-v7:25.2.0'
Better
  • 165
  • 1
  • 13
  • nope it's there. I probably wouldn't even be able to use android.support.v7.widget.CardView in my layout if that was the case. – Wirling May 12 '17 at 05:57
  • if you want put the CardView style in theme. you may custom the CardView. [this link show how to use](http://stackoverflow.com/questions/29914671/how-to-add-cardview-attributes-to-app-theme) – Better May 12 '17 at 06:28
  • I also came to that conclusion a while ago and mentioned it as a comment underneath my question. I still think it would be better if I could somehow use theming for the support-library-class instead of creating my own wrapper class. – Wirling May 15 '17 at 13:21