Development issue/problem:

In a nutshell:

How do I determine the color states of the CardView CardBackgroundColor property (here in the ListView layout)?

(I use RC1 from Android L developer preview, installed on a phone running under 4.4, and cardview-v7: 21.0.0-rc1 in build.gradle).


In the CardView layout we define the corner radius and the background color of CardView using CardCornerRadius and CardBackgroundColor.

However, the background colour does not display the selected reports, i.e. if, for example, B. clicks on an item in the list.

If you set the background color and the corresponding states observed in the internal CardView, it will be displayed above the corners you set in the CardView.

So how can we ensure that the BackgroundColor card statuses are respected by CardView?

Here is the color used for theeBackgroundColor folder, color_with_states.xml :

And a layout that uses CardView :

How can I solve this problem?

Solution 1:

This is not ideal because the edges are not rounded, but it allows you to add tactile feedback to CardView:

// Nestled view …

Android: foreground and android: clickable attributes added to CardView.

It also has the negative side effect that the android:clickable attribute replaces a clickListeners, so these clickListeners are not activated.


I have several examples of how to implement CardView.

Walk ( –

QuickReturn ( –

Update 2

After further research I found a good solution for CardViews on all versions of the API, including Lollipop previews.

@etiennelawlor/lay-out-tips-for-pre- and post-lollipop-bcb2e4cdd6b2#.9h0v1gmaw

Solution 2:

Sometimes you need visual tactile feedback from CardView. The android:foreground=?android:attr/selectableItemBackground solution is perfect for this.

However, you may want to consider using drawSelectorOnTop(true) with your ListView. This does not require any changes to your CardView.

Let me know if you need any more explanations.

Solution 3:

This is my way of solving your problem.

First create a custom class called CustomCardView expands CardView

Then we overwrite the drawableStateChanged() method, change the background color of the card by calling the setCardBackgroundColor() method when the card clicks on state.

Finally, replace CardView with this CustomCardView in your layout file.

The only drawback of this solution is that the map view on Android 5.0 and higher cannot show the ripple effect when typing.

Here’s my code:

The CustomCardView public class expands CardView {

public CustomCardView(Context) {
// TODO Automatically generated design heel

public CustomCardView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor tube

public CustomCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO auto-generation constructor stub

protected void drawableStateChanged() {
if (isPressed()) {
} otherwise {


Solution 4:

One solution I used was to customize the programmatic user interface by replacing the View.OnTouchListener OnTouch() event handler in my custom ViewHolder.

public boolean onTouch (View v, event MotionEvent)
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN)
if (action == MotionEvent.ACTION_UP | action == MotionEvent.ACTION_CANCEL)
returns falsely;

Solution No 5:

**simply add lines in the map view**.


Solution No 6:

You can use the color status in the code and it works well.


in color > card_view_selectable.xml

Then use isActivated because the background color has changed to state_activated or default.

cardView.isActivated = true // or false

Solution No 7:

I used a rectangular shape with the same angular radius as the map view. And then used the xml drawing as background for the internal map view.
The background does not appear above the corner of the map, although I always get some padding between the map and the inside view.

CardView background color states not being respected –

Solution No 8:

If you look at the definition of the carBackgroundColor property, at least the Android support library has it :

It says CardBackgroundValue only needs one colour. I think this means that the selector is ignored, but it reaches the default value, which is the color at the bottom of the selector.

Solution No 9:

Use android: foreground instead of android: background in your . example CardView code below.

// other navigation elements

Good luck!

checkable cardview android,cardview colorstatelist,custom cardview android,cardview transparent background,android style card view,android-cardview selectableitembackground,cardbackgroundcolor not working,card view background color not working,how to change cardview elevation color in android,change cardview background color programmatically,change cardview background programmatically,android set cardview background color programmatically,card view background is black,card view background color programmatically,card background color not working,cardview corner background not transparent

You May Also Like

INTRODUCTION TO MATLAB | Matlab Tutorial | Matrix

Introduction to Matlab | Matlab Tutorial – You can find the full…

How can I create an Android application in Android Studio that uses the Google Maps Api v2? –

Development issue/problem: I had many problems in Android Studio when I tried…

How To Celebrate Christmas This Year

Amidst the lights, garlands and glamour, the true meaning of Christmas is…

We take a thesis driven approach to startup investment as we look to the trends and shifts that are occuring in consumption – Sunny Midha, Principal at VC Firm Principia Growth –

Bookmark(0) Log in to favorite No bill yet? Login Sunny Midha is…