Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set button not clickable while animating on Android?

Tags:

android

button

How to make button not clickable while translate.animate is in progress. I tried with button.setclickable(false) and button.setEnabled(false) both dint worked for me.

Here is my code:

b2.setOnClickListener(new OnClickListener()
{
  public void onClick(View v)
  {
    if(i%2==0)
    {
      //b2.setClickable( false );
      //            Toast toast1 = Toast.makeText(getBaseContext(), "Success", 1);
      //            toast1.show();
      if(i==0)
      {
        //b2.setOnClickListener(null);
        b2.setEnabled(false);
        //b2.setClickable(false);
        int modifierY;
        modifierY=-70;
        Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
        translateAnimation.setDuration(600);
        translateAnimation.setFillEnabled(true);
        MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
        translateAnimation.setAnimationListener(listener);
        // b2.setOnClickListener(null);
        Animation translateAnimation1=new TranslateAnimation(0, 0, 0,0 );
        translateAnimation1.setDuration(1000);
        translateAnimation1.setFillEnabled(true);
        MyAnimationListener listener1=new MyAnimationListener(main, 0,SimpleViewPagerforAndroidActivity.this);
        translateAnimation1.setAnimationListener(listener1);

        main.setVisibility(RelativeLayout.VISIBLE);
        /*  long time =AnimationUtils.currentAnimationTimeMillis();

        main.invalidate(); 
        b2.invalidate(); 

        translateAnimation.setStartTime(time);
        translateAnimation1.setStartTime(time);*/
        main.startAnimation(translateAnimation1);

        b2.startAnimation(translateAnimation);
        i++;
        //   b2.setOnClickListener(this);
      }
      else
      {
        //b2.setOnClickListener(null);
        b2.setEnabled(false);
        //b2.setClickable(false);
        int modifierY;
        modifierY=-70;
        Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
        translateAnimation.setDuration(1000);
        translateAnimation.setFillEnabled(true);
        MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
        translateAnimation.setAnimationListener(listener);

        Animation translateAnimation1=new TranslateAnimation(0, 0, 0,-150 );
        translateAnimation1.setDuration(600);
        translateAnimation1.setFillEnabled(true);
        MyAnimationListener listener1=new MyAnimationListener(main, -150,SimpleViewPagerforAndroidActivity.this);
        translateAnimation1.setAnimationListener(listener1);

        main.setVisibility(RelativeLayout.VISIBLE);
        /*   long time =AnimationUtils.currentAnimationTimeMillis();

        main.invalidate(); 
        b2.invalidate(); 

        translateAnimation.setStartTime(time);
        translateAnimation1.setStartTime(time);*/
        main.startAnimation(translateAnimation1);

        b2.startAnimation(translateAnimation);
        //   b2.setOnClickListener(this);
        i++;
      }
    }
    else 
    {
      //b2.setOnClickListener(null);
      b2.setEnabled(false);
      //b2.setClickable(false);


      Animation translateAnimation=new TranslateAnimation(0, 0, 0,150 );
      translateAnimation.setDuration(1000);
      translateAnimation.setFillEnabled(true);
      MyAnimationListener listener=new MyAnimationListener(main, 150,SimpleViewPagerforAndroidActivity.this);
      translateAnimation.setAnimationListener(listener);
      Animation translateAnimation1=new TranslateAnimation(0, 0, 0,70 );
      translateAnimation1.setDuration(600);
      translateAnimation1.setFillEnabled(true);
      MyAnimationListener listener1=new MyAnimationListener(b2, 70,SimpleViewPagerforAndroidActivity.this);
      translateAnimation1.setAnimationListener(listener1);
      /* long time =AnimationUtils.currentAnimationTimeMillis();

      main.invalidate(); 
      b2.invalidate(); 

      translateAnimation.setStartTime(time);
      translateAnimation1.setStartTime(time);
      main.startAnimation(translateAnimation1);

      b2.startAnimation(translateAnimation);*/
      b2.startAnimation(translateAnimation1);
      main.startAnimation(translateAnimation);
      i++;
      //b2.setOnClickListener(this);
    }
  }
});
like image 648
Zombie Avatar asked May 16 '12 05:05

Zombie


2 Answers

I use button.setEnabled(false) to disable Touch event.

like image 180
user3579059 Avatar answered Oct 26 '22 23:10

user3579059


Check on button click whether animation is completed or not, using hasEnd property, check below code:

if (!(animation.hasEnded()) {
  button.setClickable(false);  
}
else{
 button.setClickable(true);  
}
like image 39
RobinHood Avatar answered Oct 26 '22 22:10

RobinHood