11

I'm running this on a Nexus 5. Here's part of the code for my CardView:

        CardView cardView = new CardView(getActivity());
        cardView.setRadius(4);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 400);
        lp.setMargins(32, 16, 32, 16);
        cardView.setLayoutParams(lp);
        cardView.setContentPadding(50, 50, 50, 50);
        ...
        cardView.setForeground(selectedItemDrawable);

And here's how I get the selectedItemDrawable:

        int[] attrs = new int[] { R.attr.selectableItemBackground };
        TypedArray ta = getActivity().obtainStyledAttributes(attrs);
        selectedItemDrawable = ta.getDrawable(0);
        ta.recycle();

When I tap the card, the ripple that's supposed to come with the selectedItemDrawable does not appear (it looks exactly the same as without the foreground set). I am running 5.0, so this seems strange, as the appcompat docs only say that it doesn't work with pre-Lollipop devices. Does anybody know why this is the case? Minimum API level is 16, targetting 21.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
Yunyu L.
  • 715
  • 9
  • 22
  • Are you sure, you don't have any view in cardview which is set to match_parent and has background of its own? – Hellboy Dec 14 '14 at 21:26
  • I have commented out any code that adds a view to the CardView, so that only the CardView is in the layout, and the results are the same. Setting that as the background to a LinearLayout with those properties in the CardView also doesn't do anything. – Yunyu L. Dec 14 '14 at 21:28
  • Indeed, when defining the CardView in XML with android:foreground="?android:attr/selectableItemBackground", there's still no ripple effect. – Yunyu L. Dec 14 '14 at 21:37
  • I'm able to create the ripple with XML with: android:clickable="true" and the selectableItemBackground foreground, but I can't seem to recreate it in code... – Yunyu L. Dec 14 '14 at 21:52

1 Answers1

9

It turned out that I was sharing my instance of the Drawable with multiple cardviews. This was resolved by returning a new instance using a getSelectedItemDrawable method:

    public Drawable getSelectedItemDrawable() {
        int[] attrs = new int[]{R.attr.selectableItemBackground};
        TypedArray ta = getActivity().obtainStyledAttributes(attrs);
        Drawable selectedItemDrawable = ta.getDrawable(0);
        ta.recycle();
        return selectedItemDrawable;
    }

Then setting it as the foreground programatically:

        cardView.setForeground(getSelectedItemDrawable());
        cardView.setClickable(true);

Now I get the ripple effect on 5.0.

Yunyu L.
  • 715
  • 9
  • 22
  • Confirmed, setForeground() is only available on API 23+ - the Android docs incorrectly have it listed as API Level 1. AOSP bug report here - https://code.google.com/p/android/issues/detail?id=186273. – Sean Barbeau Oct 27 '15 at 14:34
  • This did not work for me. I tried putting this directly on an image view and also on a frame layout containing the image view. Neither worked. – Clive Jefferies Aug 12 '16 at 15:53
  • you have to also add android:clickable="true" on your cardView – Dagnogo Jean-François May 10 '17 at 11:13