ObjectAnimator Proxy to Animate TopMargin can't find setting/getter

android, c#, xamarin, xamarin.android

Solution

You need to add the [Export] attribute to your getTopMargin and setTopMargin methods. E.G.

[Export]
public int getTopMargin()
{
  var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
  return lp.TopMargin;
}

[Export]
public void setTopMargin(int margin)
{
  var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
  lp.SetMargins(lp.LeftMargin, margin, lp.RightMargin, lp.BottomMargin);
  mView.RequestLayout();
}

The [Export] attribute also requires that you add a reference to the Mono.Android.Export assembly.

Documentation:

http://androidapi.xamarin.com/?link=T%3aJava.Interop.ExportAttribute

Problem

We are trying to use an objectanimator proxy to animate the TopMargin property in Android (Xamarin). However, we get this error: [PropertyValuesHolder] Couldn't find setter/getter for property TopMargin with value type float Note: we tried TopMargin, topMargin, GetTopMargin, getTopMargin, etc., thinking that it might be an issue of casing conversion betwen Java and C#, but it doesn't look to be the case. Our code in the Activity starting the animation: ``` translation = new int[] {0, 300}; var anim2 = ObjectAnimator.OfInt( new MarginProxyAnimator(myview), "TopMargin",translation); anim2.SetDuration(500); anim2.Start(); ``` Our proxy class: ``` public class MarginProxyAnimator : Java.Lang.Object { ///... other code... public int getTopMargin() { var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters; return lp.TopMargin; } public void setTopMargin(int margin) { var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters; lp.SetMargins(lp.LeftMargin, margin, lp.RightMargin, lp.BottomMargin); mView.RequestLayout(); } } ``` Any advise? a pointer to a working Xamarin sample using a proxy would be helpful. thanks.

Original source