I'm working on animation and I want to organize my code in best way I'm new in android and not know too much about this. Here is my code that I'm using to animate views


   public class TestClick implements OnClickListener {

Context context;
ImageView iv1,iv2,iv3;
public static boolean bActive = false;

public TestClick(Context ctx, ImageView imageView1, ImageView imageView2, ImageView imageView3){
    context = ctx;
    iv1 = imageView1;
    iv2 = imageView2;
    iv3 = imageView3;

public void onClick(View v) {
    if(bActive) {
        Animation animPictureDeactiveLeft = AnimationUtils.loadAnimation(context, R.anim.deactivate_picturebtn_onleft);
        Animation animVideoMiniLeft = AnimationUtils.loadAnimation(context, R.anim.activate_videobtn_onleft);
        Animation animGifMiniLeft = AnimationUtils.loadAnimation(context, R.anim.move_gifbtn_onleft);

        bActive = true;

public void active(){




  button1.setOnClickListener(new TestClick(MainActivity.this, view1, view2, view3){
        public void active(){
            // do some work
   button2.setOnClickListener(new TestClick(MainActivity.this, view1, view2, view3){
        public void active(){
            // do some work

This code is working fine there is no problem in this.
What I need Actually I need to manage my all animation in separate file. I don't want to write whole code in MainActivity it's not look good.

  1. As you can see in my code I'm passing views in constructor is it a good way passing two many views in constructor ?

  2. Animations load in onClick override method everytime when onClick called animation load again Right ?

  3. As you can see button2 also need same animation I want when button1 is clicked it load animations but when button2 click don't load again animation just use the previous loaded animation is it possible if yes how ?

  4. Is there any heap memory problem by passing views in such manner.

I'm asking these questions because I'm new to Android. If there is another method to achieve this please let me know. I want efficient code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.