Android CardView apply Theme
S

2

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>
Sundsvall answered 3/8, 2016 at 7:45 Comment(2)
Does this SO question answer your question too?Countershaft
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 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.Sundsvall
V
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>
Valaree answered 4/8, 2020 at 13:42 Comment(2)
Is there a big list with all the style names so that we can find out how each element is called ?Inflate
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.Valaree
U
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'
Uproar answered 12/5, 2017 at 2:13 Comment(3)
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.Sundsvall
if you want put the CardView style in theme. you may custom the CardView. this link show how to useUproar
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.Sundsvall

© 2022 - 2024 — McMap. All rights reserved.