Issue
I have many buttons in my MainActivity.xml and i wanted to add an animation to all of them such that when the MainActivity is launched all buttons get a TRANSLATIONX animation to bring them into view. I have succesfully created the Button array using button as a data type and added three buttons to it as a sample... The problem however is iterating through the button array to add animation property to each... Here is the trier code i used to reach where i am, Suggestions and Solutions are greatly appreciated...
class Prime : AppCompatActivity
{
//Creating button array
Button[] mybuttons;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.Prime);
//Button definitions
Button button1 = this.FindViewById<Button>(Resource.Id.button1);
Button button2 = this.FindViewById<Button>(Resource.Id.button2);
Button button3 = this.FindViewById<Button>(Resource.Id.button3);
//Adding the buttons to an array
mybuttons = new Button[3] { button1, button2, button3 };
//Defining a view animation
ObjectAnimator animator = ObjectAnimator.OfFloat(button3, "translationX", 130f);
animator.SetDuration(2000);
animator.Start();
//This is where i need the loop code to iterate through the button array and to each of them them the animation object property
I tried replacing the button3
parameter in the ObjectAnimator.OfFloat
method with mybuttons
but it didnt work. Thanks
Solution
The loop foreach
works fine to access every button in the array and style it with respect to the Object Animator.
foreach(Button button in mybuttons){
ObjectAnimator animator = ObjectAnimator.OfFloat(button, "translationX", 130f);
animator.SetDuration(2000);
animator.Start();
}
The foreach loop is easy to understand and implement for Button arrays than its counter parts
Answered By - user14187680
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.