Programmatically adding animation effect to a (programmatically added) popupWindow in android

Vlad picture Vlad · Nov 26, 2014 · Viewed 12k times · Source

So, I have a programmatically added PopupWindow which looks like this:

        dialog = new PopupWindow(context);
        dialog.setContentView(ll);
        dialog.showAtLocation(view, Gravity.LEFT | Gravity.TOP, -70, 0);
        dialog.setWidth(w);
        dialog.setHeight(h - 50);
        dialog.setOutsideTouchable(true);
        //The dialog.update is somewhere else, I didn't bother adding it too as it is not important for this matter (I guess)

What I want to do is to have some sort of animation effect , like it pops right from the button I press so the popup appears. (this is just an example, I just want any sort of animation).

Documentation would be ok too, as long as it is not XML based (I found those already -not really helping me).

If other details are needed ,I will comment or edit the question.

Answer

Vlad picture Vlad · Nov 27, 2014

So, I managed to deal with this problem.

There are three simple steps to achieve the animation effect.

First: Make two XMLs that are the animation.In my case were those two following down here. animation_on.xml

 <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" />

animation_off.xml

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

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

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


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

Second:

<style name="animationName" parent="android:Animation">
    <item name="android:windowEnterAnimation">@anim/animation_on</item>
    <item name="android:windowExitAnimation">@anim/animation_off</item>
</style>

Third:

 dialog = new PopupWindow(context);
 // ....other code, whatever you want to do with your popupWindow (named dialog in our case here)
 dialog.setAnimationStyle(R.style.animationName);

If anyone needs help with this, leave comment.I will answer as fast as I can.