12

I has a nice PopupWindow which I want to appear with an animation. I do it like this:

popup.setAnimationStyle(R.anim.appear);
popup.showAtLocation(popupMenuLayout, gravity, offsetX, offsetY);

I then set up a listener for changing the animation:

popup.setOnDismissListener(new PopupWindow.OnDismissListener(){
 @Override
 public void onDismiss(){
  popup.setAnimationStyle(R.anim.disappear);
 }      
}); 

But, hey, it won't work. Neither for res/anim/appear:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromYDelta="100%"
 android:toYDelta="0"
 android:duration="1000" 
/>

Nor for res/anim/disappear:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromYDelta="0"
 android:toYDelta="100%"
 android:duration="1000"
/>

Any clues?

slipbull
  • 1,477
  • 1
  • 11
  • 27

1 Answers1

53

Actually, PopupWindow.setAnimationStyle expects a style with 2 entries. You'll need to have two xmls, each containing a <set>, one for showing and the other for hiding the window. When this is done, put the following piece into values/styles.xml:

<style name="AnimationPopup">
    <item name="android:windowEnterAnimation">@anim/popup_show</item>
    <item name="android:windowExitAnimation">@anim/popup_hide</item>
</style>

and set your animation style to R.style.AnimationPopup. That'll do.

I've got this information from https://github.com/lorensiuswlt/NewQuickAction3D the documentation didn't seem to mention it.

Update:

An update to Android SDK in 2012 have changed XML syntax. The original @android:windowEnterAnimation now became android:windowEnterAnimation. So this answer is updated accordingly.

He Shiming
  • 5,710
  • 5
  • 38
  • 68
  • 2
    How about accepting this as the answer if it has helped you. So that someone else could easily get to know that this works. – Andro Selva Sep 15 '12 at 11:08
  • One error in your sample : it should be "android:windowEnterAnimation" and not "@android:windowEnterAnimation" Same for windowExitAnimation – Foxykeep Jan 29 '13 at 23:08
  • The Android documentation is really lame at some points, thank you for that very good input! – Thibault D. Mar 12 '13 at 13:57