Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach custom CardView style to theme

Tags:

In my app, I have two themes (light and dark), and I want all of my CardViews to change their background color depending on which theme is selected.

What I don't want is:

<android.support.v7.widget.CardView     style="@style/CardView.MyBlue"     android:layout_width="200dp"     android:layout_height="100dp"     android:layout_gravity="center_horizontal"> 

The above code is not dynamic. I need my two styles to automatically be applied depending on if a light or a dark theme is selected.


What I have right now doesn't work:

<style name="AppTheme.Light" parent="Theme.AppCompat.Light">    ...    <item name="cardViewStyle">@style/CardViewStyle.Light</item> </style> <style name="AppTheme.Dark" parent="Theme.AppCompat">    ...    <item name="cardViewStyle">@style/CardViewStyle.Dark</item> </style>  <style name="CardViewStyle.Light" parent="CardView">     <item name="cardBackgroundColor">@color/cardview_dark_background</item> </style>  <style name="CardViewStyle.Dark" parent="CardView">         <item name="cardBackgroundColor">@color/cardview_light_background</item> </style> 

I read somewhere that you can define a styleable.xml file in /res/values/ to key the word cardViewStyle, so I did that:

styleable.xml:

<resources>     <declare-styleable name="AppTheme">         <attr name="cardViewStyle" format="reference" />     </declare-styleable> </resources> 

Update

Similar question here with no answer either.

like image 950
sameetandpotatoes Avatar asked May 23 '15 21:05

sameetandpotatoes


People also ask

How do I customize my CardView?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

How do I add a CardView library?

To add a dependency on CardView, you must add the Google Maven repository to your project. Read Google's Maven repository for more information. For more information about dependencies, see Add build dependencies.

What is material Card view in Android?

MaterialCardView is a customizable component based on CardView from the Android Support Library. MaterialCardView provides all of the features of CardView , but adds attributes for customizing the stroke and uses an updated Material style by default.


2 Answers

In styles.xml

<resources>      <attr format="reference" name="cardStyle"/>      <style name="Light" parent="Theme.AppCompat.NoActionBar">         <item name="cardStyle">@style/CardView.Light</item>         ...     </style>      <style name="Dark" parent="Theme.AppCompat.NoActionBar">         <item name="cardStyle">@style/CardView.Dark</item>         ...     </style> </resources> 

Then in your other xml to use the new attribute, you would use it like this

style="?attr/cardStyle" 
like image 61
David Park Avatar answered Oct 10 '22 05:10

David Park


To show more detail about how to implement David Park's solution...

Put this in attrs.xml:

<declare-styleable name = "cardStyle">      <attr name="cardStyle" format="reference" /> </declare-styleable>  <style name="Light" parent="Theme.AppCompat.NoActionBar">     <item name="cardStyle">@style/CardView.Light</item> </style>  <style name="Dark" parent="Theme.AppCompat.NoActionBar">     <item name="cardStyle">@style/CardView.Dark</item> </style> 

Then, add cardStyle to your theme in styles.xml:

<style name="AppThemeLight" parent="AppTheme.Base"/> <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">     <item name="cardStyle">@style/CardView.Light</item> </style>  <style name="AppThemeDark" parent="AppTheme.Base.Dark"/> <style name="AppTheme.Base.Dark" parent="Theme.AppCompat.NoActionBar">     <item name="cardStyle">@style/CardView.Dark</item> </style> 

Then use the attr wherever you have a CardView:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:card_view="http://schemas.android.com/apk/res-auto"     android:id="@+id/header_card"     android:layout_width="match_parent"     android:layout_height="wrap_content"     card_view:cardCornerRadius="2dp"     card_view:cardElevation="2dp"     style="?attr/cardStyle">      <TextView xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:gravity="center_vertical"         android:id="@+id/header_title"  </android.support.v7.widget.CardView> 
like image 33
flar2 Avatar answered Oct 10 '22 05:10

flar2