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

So, I have a programmatically added PopupWindow that 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 kind of animation effect, for example, it appears to the right of the button that I click on so that a pop-up window appears. (this is just an example, I just want any animation).

The documentation will also be fine if it is not XML-based (I found that they no longer help me).

If you need other data, I will comment or edit the question.

+8
android popupwindow
source share
2 answers

So, I managed to deal with this problem.

There are three easy steps to achieve an animation effect.

First: Make two XML files that are animations. In my case, these were the next two. 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" /> 

Secondly:

 <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 a comment. I will answer as soon as I can.

+14
source share

Here is the code to customize the animation style. Before calling showAtLocation, make sure you call the setAnimationStyle method.

 dialog = new PopupWindow(context); dialog.setAnimationStyle(android.R.style.Animation_Dialog); 

Hope this helps.

Link: setAnimationStyle

+9
source share

All Articles