Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make animation for popup window in android

I hava a popup window in my application, its appears when some button clicked I want to set fade in animation to this window, I put the xml file in "res/anim" folder and set the animation style for the popup window but the animation does not work? here is my codes:

myanim.xml...

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">     <alpha android:fromAlpha="0.0"         android:toAlpha="1.0"          android:interpolator="@android:anim/accelerate_interpolator"          android:duration="4000"         android:repeatCount="1"/> </set> 

===============================================

Create the popup window

private PopupWindow showOptions(Context mcon){     try{          LayoutInflater inflater = (LayoutInflater) mcon.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);         View layout = inflater.inflate(R.layout.options_layout,null);         layout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.myanim));         PopupWindow optionspu = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);          optionspu.setFocusable(true);         optionspu.showAtLocation(layout, Gravity.TOP, 0, 0);         optionspu.update(0, 0, LayoutParams.WRAP_CONTENT, (int)(hei/5));         optionspu.setAnimationStyle(R.anim.myanim);         return optionspu;     }     catch (Exception e){e.printStackTrace();     return null;} } 

================================================= onClick method... (optionsPopup is global variable of type PopupWindow)

 @Override public void onClick(View v) {                switch (v.getId()) {          case R.id.options:                 optionsPopup=showOptions(this);             break;   } 
like image 471
Ishaq Avatar asked Feb 12 '12 09:02

Ishaq


People also ask

How do I create a pop up screen on my Android?

Use setWidth(int) and setHeight(int) . Set the layout type for this window. Display the content view in a popup window anchored to the bottom-left corner of the anchor view. Displays the content view in a popup window anchored to the corner of another view.

How do I set up animator on Android?

Navigate to the app > res > Right-Click on res >> New >> Directory >> Name your directory as “anim”. Inside this directory, we will create our animations. For creating a new anim right click on the anim directory >> Animation Resource file and give the name to your file.

Is animation possible on Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.


2 Answers

I think the problem is you have provided only one set of animation style. But actually a PopupWindow requires two animations. One will be used by it when window is shown and other one to hide the window.

This is how you should do it,

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.

2) Now inside values folder create a xml called styles.xml and add these animations to it like this,

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

3) Now set this style to your PopupWindow animation,

 popup.setAnimationStyle(R.style.Animation); 

Now it automatically detects window enter and exit and provides with the required animation.

like image 113
Andro Selva Avatar answered Oct 07 '22 10:10

Andro Selva


i am using a popup animation with this code:

// Creating the PopupWindow        layoutInflater = (LayoutInflater)     getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);         inflatedLayoutView = layoutInflater.inflate(R.layout.packages_popup,null);      inflatedLayoutView.setAnimation(AnimationUtils.loadAnimation(this, R.animator.popupanim)       popup_l = new PopupWindow(inflatedLayoutView);     popup_l.setWidth(FrameLayout.LayoutParams.WRAP_CONTENT);    popup_l.setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);         popup_l.setFocusable(true);    // Clear the default translucent background    popup_l.setBackgroundDrawable(new BitmapDrawable());            popup_l.showAtLocation(parent, Gravity.CENTER, 0 , 0);        popup_l.setOutsideTouchable(false); 

located in /res/animator/popupanim.xml (popupanim.xml) the animation code is:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >  <alpha android:fromAlpha="0.0"         android:toAlpha="1.0"          android:interpolator="@android:anim/accelerate_interpolator"          android:duration="500"         android:repeatCount="0"/> </set> 
like image 30
aimiliano Avatar answered Oct 07 '22 09:10

aimiliano