Xamarin.Android How to set the ripple effect programmatically on any view?

我怕爱的太早我们不能终老 提交于 2019-12-12 12:44:21

问题


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);

回答1:


Well, the smart way of doing it would be something like this :

Note: The following code does not work in devices running below API-21 or Android Lollipop.

Add the following XML to your layout folder.

Ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/yourViewRippleColor"
tools:targetApi="lollipop">

<item>
<color android:color="@color/yourViewBackgroundColor" />
</item>

<item android:id="@android:id/mask">
  <shape android:shape="rectangle">
  <solid android:color="@color/yourViewRippleColor" />
  </shape>
</item>
</ripple>

And use it like this when needed:

_yourView.SetBackgroundResource(Resource.Layout.Ripple);

And if I am not wrong you can do it purely programmatically using something like this :

Note: Should work on any device above and on honeycomb.

TypedValue outValue = new TypedValue();
        this.Theme.ResolveAttribute(Android.Resource.Attribute.SelectableItemBackground, outValue, true);//In an Activity
        this.Activity.Theme.ResolveAttribute(Android.Resource.Attribute.SelectableItemBackground, outValue, true);//In an Fragment
        _YourView.SetBackgroundResource(outValue.ResourceId);

Goodluck!

In case you have queries revert.



来源:https://stackoverflow.com/questions/50174375/xamarin-android-how-to-set-the-ripple-effect-programmatically-on-any-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!