Reputation: 17139
There is no method to get the current background color of CardView. There is a method to set the background like
cardView.setCardBackgroundColor(color);
I would like something like:
cardView.getCardBackgroundColor();
It will be really helpful.
Upvotes: 2
Views: 884
Reputation: 39201
It looks like a getCardBackgroundColor()
method has indeed been added to CardView
, but in a rather recent version, so just make sure your support library version is up to date.
Do note that this method returns a ColorStateList
rather than a single color value. Calling getDefaultColor()
on that will give you the normal background color.
int backgroundColor = cardView.getCardBackgroundColor().getDefaultColor();
Upvotes: 5
Reputation: 17139
It was a problem with gradle. I was using an old version of CardView bundled with some other library. Fixed by adding the line:
compile 'com.android.support:cardview-v7:24.2.1'
Upvotes: 0
Reputation: 5158
You should set and get color to parent layout inside CardView.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
</RelativeLayout>
In this case, try to set and get from RelativeLayout.
Upvotes: 0