Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make code wait untill an animation ends in android?

I have an EditText with android:maxLength="17" and used buttons with onClickListener to update text of this EditText the process here is that when the text length reaches to 16 the EditText animate's towards left with distance of approx one character using TranslateAnimation(0, -15, 0, 0); and then the first character of EditText is being removed. for this first i call the method to perform animation and then call method to remove the the first characer like shown below

animate();
removeChar();

The problem here is that before the end of animation the character got removed, means while the animation is running the removeChar() completes its functionality while i want the removeChar() to perform its functionality after animation ends.

when i googled for this i found answer to use animation listener but while writing code in Eclips i don't found any animation listener for the object

the methods have no error and perform their functionality accurate as i want. the code for methods is show below

 public void animate()
{
                //slide is declared at class level
                slide = new TranslateAnimation(0, -15, 0,0 );   
                slide.setDuration(1250);   
                slide.setFillAfter(true);   
                myEditText.startAnimation(slide);
}


public void removeChar()
{           
    String update="";
    for(int i=1 ; i < myEditText.getText().toString().length() ; i++)
    {
        update=update+input.getText().toString().charAt(i);
    }
    myEditText.setText(update);
}

I have also tried to use the code below to wait for end of animation but it halts the application .

            animate();
            //slide is declared at class level
            while(!(slide.hasEnded()))
               {
                    // Just waste the time
               }
            removeChar();

i think the application halts because all processing goes for while loop and animation doe'nt gots ended and hence loop becomes an infinite loop and causes application to halt

i searched a lot and found no proper answer plz help me and i will prefer a code snippet or a little example as I'm new to android plz really need help...

like image 858
Umar Qureshi Avatar asked Sep 28 '11 13:09

Umar Qureshi


People also ask

How do I slow down animation on Android?

Open the Settings app, if it's not already open, by swiping down from the top of the screen once (or twice, depending on the brand of device you're using) and tapping the Settings (gear) icon. To speed up, slow down, or disable the animations on your Android device, the Developer options have to be available.

Is animation possible on Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.


1 Answers

Attach an AnimationListener to your animation and then execute removeChar() in the onAnimationEnd() method. Like this:

slide.addAnimationListener(new AnimationListener(){
    public void onAnimationStart(Animation a){}
    public void onAnimationRepeat(Animation a){}
    public void onAnimationEnd(Animation a){
        NameOfParentClass.this.removeChar();
    }

});
like image 81
slayton Avatar answered Sep 20 '22 16:09

slayton