Droidman
Droidman

Reputation: 11608

wait for animation to complete before hiding View

I perform some things triggered by a Button click:

    private void onSearchPressed() {

    title.setVisibility(View.GONE);
    etActionSearch.setVisibility(View.VISIBLE);
    etActionSearch.requestFocus();
    btnActionFavs.setVisibility(View.GONE);
    etActionSearch.setAnimation(AnimationUtils.loadAnimation(
            getApplicationContext(), R.anim.et_anim_open));
    isSearch = true;

}

So basically I just hide some Views and show some others, my EditText is "sliding out" using a simple set Animation.

When the action is cancelled, I reverse the process:

    private void onSearchCancelled() {
            etActionSearch.setAnimation(AnimationUtils.loadAnimation(
            getApplicationContext(), R.anim.et_anim_close));
    etActionSearch.setVisibility(View.GONE);
    btnActionFavs.setVisibility(View.VISIBLE);
    title.setVisibility(View.VISIBLE);
    isSearch = false;

}

What I'd like to do is to apply an Animation (opposite direction) to my EditText, so it disappears also with a slide animation. The problem is that all the code is executed immediately, so the EditText is gone BEFORE its animation is complete. I tried some weird things like using an AsyncTask and putting the animation inside the doInBackground() method, setting the Visibility of the Views in onPostExecute() but that didnt change anything.. SystemClock.sleep() also doesn't do anything but a lag impression. Any solutions?

Upvotes: 1

Views: 1005

Answers (2)

vilpe89
vilpe89

Reputation: 4702

Like Marcin Orlowski said, you have to use AnimationListener with your Animation. After the animation is ended it will launch the onAnimationEnd() event where you can do your stuff.

Here's a little example what i could look like:

Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.your_specific_animation);
animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // Do your hiding stuff here
        }
    });

etActionSearch.startAnimation(animation);

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Animations run asynchronously, so you need to use AnimationListener on that animation and put your code that should be executed when animation is over to onAnimationEnd().

See docs: http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

Upvotes: 2

Related Questions