6

I want to set the foreground of my CardView to ?attr/selectableItemBackground.

It work's when I declare the foreground in xml:

android:foreground="?attr/selectableItemBackground"

But I want to set the foreground programmatically. This doesn't work:

int[] attrs = new int[]{R.attr.color_a, R.attr.selectableItemBackground};
TypedArray ta = context.obtainStyledAttributes(attrs);
mColorA = ta.getColor(0, 0);
mSelectableItemBackground = ContextCompat.getDrawable(context, ta.getResourceId(1, 0));
ta.recycle();

...

cardView.setOnClickListener(onClickListener);
cardView.setClickable(true);
cardView.setForeground(mSelectableItemBackground);

However, retrieving the attributes works (mSelectableItemBackground contains a RippleDrawable), but the foreground doesn't change when I press on the Card.

Auroratic
  • 462
  • 8
  • 25
  • I think I just found a possible solution here: http://stackoverflow.com/q/27474279/4752807 .. I too use the same instance on multiple cards.. I have to investigate and will update this question then. – Auroratic Nov 14 '15 at 01:15
  • Have you seen this answer? http://stackoverflow.com/a/30192562/4409409 – Daniel Nugent Nov 14 '15 at 01:21

1 Answers1

2

The issue was that I used the same Drawable with multiple Cards.

You have to retrieve a new Drawable for each Card.

More details in this answer.

Community
  • 1
  • 1
Auroratic
  • 462
  • 8
  • 25