Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 244
Vlad Avatar asked Nov 26 '14 11:11

Vlad


People also ask

How do you add an animation to a pop-up window?

1) Create Two Different set of animations. say, popup_show. xml and popup_hide. xml and add it to your anim folder which you have to create inside res folder.

What is pop-up window in Android?

android.widget.PopupWindow. This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.


1 Answers

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.

like image 154
Vlad Avatar answered Oct 23 '22 05:10

Vlad