Sritam Jagadev
Sritam Jagadev

Reputation: 975

Click operation not working in android button after applying move animation to it in Android

Guys i have designed view, where i have added three buttons in that view. I have given on_click facility to all the three buttons. My requirement is if i am clicking on button_1, then button_2 and button three should be moved to a new location. but, the problem is that,after applying move animation to button_2,button_3 on_click facility not working on them in that new position.

MainActivity.java

public class MainActivity extends Activity {

ImageButton slider,slidernew,sliderexisting;
boolean flag1 = true;
boolean flag2 = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //final LinearLayout container = (LinearLayout) findViewById(R.id.container);
    slider = (ImageButton) findViewById(R.id.imageButton1);
    slidernew = (ImageButton) findViewById(R.id.imageButton2);
    sliderexisting = (ImageButton) findViewById(R.id.imageButton3);

    // Set long default duration for the animator, for the purposes of this demo
    //animate(slidernew).setDuration(2000);
    slider.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*int xValue = container.getWidth() - slidernew.getWidth();
            int yValue = container.getHeight() - slidernew.getHeight();
            animate(slidernew).x(xValue).y(yValue);*/
            if(flag1==true)
            {
                animateSliderForward();
            flag1=false;
            }
            else{
                animateSliderBackward();
                flag1=true;
            }
        }
    });

    slidernew.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(MainActivity.this,SecondActivity.class));

        }
    });
    sliderexisting.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(MainActivity.this,SecondActivity.class));

        }
    });
}

void animateSliderForward(){
    float fromX=0;
    float toX=100;
    float fromY=0;
    float toY=100;
    TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
    animation.setDuration(1000);
    animation.setFillAfter(true);
    float fromA=0;
    float toA=-100;
    float fromB=0;
    float toB=100;
    TranslateAnimation animation1 = new TranslateAnimation(fromA, toA, fromB, toB);
    animation1.setDuration(1000);
    animation1.setFillAfter(true);
    slidernew.startAnimation(animation1);
    sliderexisting.startAnimation(animation);
}
   void animateSliderBackward(){
   float fromX=100;
   float toX=0;
   float fromY=100;
   float toY=0;
   TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
   animation.setDuration(1000);
   animation.setFillAfter(true);
   float fromA=-100;
   float toA=0;
   float fromB=100;
   float toB=0;
   TranslateAnimation animation1 = new TranslateAnimation(fromA, toA, fromB, toB);
   animation1.setDuration(1000);
   animation1.setFillAfter(true);
   slidernew.startAnimation(animation1);
   sliderexisting.startAnimation(animation);
   }


}

Upvotes: 5

Views: 919

Answers (2)

Gent Ahmeti
Gent Ahmeti

Reputation: 1599

Consider using a Property Animation.

From the above document.

Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

Using the property animations will solve this issue.

Upvotes: 2

Bala Vishnu
Bala Vishnu

Reputation: 2628

You have used onClickListener for both animation and moving to next activity. Change one to onTouchListener..make sure you return false in the onTouchListener so that onClick executes

Upvotes: 1

Related Questions