Binoy Babu
Binoy Babu

Reputation: 17139

There is no method to get the current background color of CardView

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

Answers (3)

Mike M.
Mike M.

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

Binoy Babu
Binoy Babu

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

Cătălin Florescu
Cătălin Florescu

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

Related Questions