BrunoM24
BrunoM24

Reputation: 41

How to only dismiss a popupWindow after animation end

I know that exist a topic similar with this Question but I cant figure how to make this work with my code.

I have one popupwindow, (I dont even know if this is the best way to do what I want but Im new on Android), but I have done a popupWindow appear on click of a image with a animation that I found here, after I read a Question about how to animate the popupwindow and that I have todo the animation of the Enter and the Exit, I figure how to do that, but now my Question:

I dont know how to do the popupwindow dismiss after the Enter Animatio ends.

popupWindow code:

LayoutInflater inflater =  (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View viewAnimationResourceEarned = inflater.inflate(R.layout.layout_animation_resources_earned, null);

animationResourceEarned = new PopupWindow(viewAnimationResourceEarned, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
animationResourceEarned.setAnimationStyle(R.style.styleAnimationResourceEarned);
animationResourceEarned.showAtLocation(viewAnimationResourceEarned, Gravity.CENTER, 0, 0);

Enter Animation code:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">

    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:toXScale="1.0"
        android:fromXScale="0.0"

        android:toYScale="1.0"
        android:fromYScale="0.0"

        android:pivotX="0%"
        android:pivotY="50%"

        android:startOffset="100"
        android:duration="300" />

    <translate
        android:fromYDelta="0"
        android:toYDelta="-50"
        android:duration="2500"/>
</set>

I want to dismiss the popup window after the Enter Animation end, My objective is the popupappear, move to the top a litle and diseppear

Upvotes: 0

Views: 1524

Answers (2)

Ladislav Havlik
Ladislav Havlik

Reputation: 31

Bruno,

Faced the same issue. Unfortunately there is currently no way to get a callback to animation used in popupwindow. I did a small workaround using countdowntimer. Not perfect - but works well. Hope it helps.

    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            new CountDownTimer(animationDuration, 10) {
                public void onTick(long millisUntilFinished) {
                }
                public void onFinish() {
                    toggle();
                }
            }.start();
        }
    });

Additionally to get the animation duration from resource file use following code

    int[] attrs = {android.R.attr.windowExitAnimation};
    TypedArray ta = context.obtainStyledAttributes(R.style.PopUpMenuAnimation, attrs);
    int resID = ta.getResourceId(0,0);
    Animation anim = AnimationUtils.loadAnimation(context, resID);
    final long animationDuration = anim.getDuration(); 

Upvotes: 0

Abhriya Roy
Abhriya Roy

Reputation: 1438

You can use the animation listener.

You have to define the animation in an animation variable like this

Animation animation = AnimationUtils.loadAnimation(this, R.style.styleAnimationResourceEarned);

And then,

    animation.setAnimationListener(new Animation.AnimationListener() { 

@Override public void onAnimationStart(Animation animation) { 

} 

@Override public void onAnimationEnd(Animation animation) {
//Dismiss it
 } 

@Override public void onAnimationRepeat(Animation animation) { 

} });

Upvotes: 1

Related Questions