Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android CardView apply Theme

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>
like image 997
Wirling Avatar asked Aug 03 '16 07:08

Wirling


1 Answers

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>
like image 87
J-Jamet Avatar answered Sep 19 '22 18:09

J-Jamet