First time asking a question here so lets see...
I'm having trouble with setting the ripple effect programmatically onto a CardView. (But i hope to find a way that works basically on any kind of view) The thing is, my cards are made programmatically like this :
...
//make cardview
CardView result = new CardView(Activity);
//set layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 100, 1f);
layoutParams.SetMargins(10, 10, 10, 10);
result.LayoutParameters = layoutParams;
result.Tag = itemId.ToString();
//FAILED ATTEMPT 1:
//result.Foreground = "?android:attr/selectableItemBackground";
//FAILED ATTEMPT 2 :
//result.SetBackgroundDrawable(view.Resources.GetDrawable(Resource.Drawable.ripple));
...
Now as you can see i tried it with the foreground property based on the answer to a similar question that can be found here.
the second attempt makes me feel like its on the right path but it makes all my cards invisible-ish : link. (I added the ripple.xml to the drawable folder of my project)
I also found the RippleDrawable class but i really don't understand how to use it correctly. It asks for using a mask and a content drawable but i have no idea what to put there. My implementation of that so far :
result.Background = new RippleDrawable(view.Resources.GetColor(Resource.Color.green),????,?????);
The main reason i want the ripple effect is because i show a list of cards, and they all have a onLongClick event that opens a popupmenu. I want to indicate that the cards are clickable.
Anyways, I hope somebody can help me find a solution.
**UPDATE : ** cards turn invisible with code of pre-android 5.
...
result.Tag = itemId.ToString();
TypedValue outValue = new TypedValue();
this.Activity.Theme.ResolveAttribute(Android.Resource.Attribute.SelectableItemBackground, outValue, true);
result.SetBackgroundResource(outValue.ResourceId);