PopupWindow animation not working - android

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?

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.

Related

Animate Views when Activity starts in Android

I am finishing FlashScreen and starting MainActivity using startActivity();
and I want to slide linearLayout of MainActivity from left to right slowly like an animation. But it's not happening. At first the linearLayout loads up and then it performs the animation so it's of no use.
Here's my code :
slide_from_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
MainAcitivity.java
Animation animFadein = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_from_left);
mLinearLayoutParent.startAnimation(animFadein);
Please help me with this.
Thanks
if you are sliding from any side, then this View should be "slided out" in this direction when initialised (on start). consider adding proper param to XML, in your case it would be android:translationX for mLinearLayoutParent. use some very high value to be shure that this View isn't visible, as in here you can't set translation by % of view like in <translate tag. when anim starts then android:fromXDelta="-100%" line will move View to this position (exact -100% of width, as initial for animation) at very beginning of animation
To animate every activity when it opens or closes, you need to create four animation files (namely : slide_in_right, slide_in_left, slide_out_right, slide_out_left). After creating these animations, you will have to add the following code in your values/styles.xml:
<style name="android:customActivityAnimation" parent="#android:style/Animation.Activity">
<item name="activityOpenEnterAnimation">#anim/slide_in_right</item>
<item name="activityOpenExitAnimation">#anim/slide_out_left</item>
<item name="activityCloseEnterAnimation">#anim/slide_in_left</item>
<item name="activityCloseExitAnimation">#anim/slide_out_right</item>
</style>
After that in your default application theme add this style :
<style name="android:windowAnimationStyle">#style/customActivityAnimation</style>

What is the default transition between activities in Android 4.0 (API 14+)

I built an application with quite a few activities and I would like to have "slide from right on enter/slide from left on exit" transitions between them.
I read more than once that slide transitions should be the Android default, but on the device I am developing on the transitions are fade in/fade out by default (Galaxy Tab 2 7", on ICS 4.0).
Is there anything I need to declare at application level, for example in the manifest file?
I am asking because otherwise I would need to add overridePendingTransition (R.anim.right_slide_in, R.anim.left_slide_out); to all my transitions which are plenty...just wondering if I am missing something before going that road.
Many thanks
No answers...on the devices 4+ I tried, the animation is a fade-in fade-out with zoom in or out...
I added the code manually where I wanted to have the slide animation:
//open activity
startActivity(new Intent(this, MyActivity.class));
overridePendingTransition(R.anim.right_slide_in, R.anim.left_slide_out);
xml animation right to left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<translate
android:duration="300"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
xml animation left to right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
In your style.xml file put
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">#android:anim/slide_in_left</item>
<item name="android:windowExitAnimation">#android:anim/slide_out_right</item>
</style>
and add
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
...
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
There is a way which involves creating a style and then adding it in manifest here:
How to change all the activity transitions at once in Android application?,
yet I only managed to make it work over startActivity(). Whenever back is pressed this method won't work or I might just missing something.

Dialog animation fillAfter doesn't work

I can't manage to get the fillAfter property of an animation to work.
I used it both in the translate and the set of the animation in xml but it always jumps to the standard dialog position at the end. Is this even possible?
Current slide in animation in xml:
<set
android:fillAfter="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:toXDelta="70%"
android:fromYDelta="100%"
android:toYDelta="70%"
android:duration="2000" />
</set>
The Themes.xml that is being applied to the dialogs:
<style name="theme_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">#style/style_slide_in_dialog</item>
</style>
<style name="style_slide_in_dialog">
<item name="android:windowEnterAnimation">#anim/animation_slide_in_dialog</item>
<item name="android:windowExitAnimation">#anim/animation_slide_out_dialog</item>
</style>
Animations like TranslateAnimation are temporary. If you want the effect to persist when the animation is complete, you need to register an AnimationListener and do something to make the change permanent in onAnimationEnd(). For example, with a TranslateAnimation, you would need to adjust your LayoutParams to have it reside in your final position.

setAnimationStyle is not working on Android 2.3.5

I wrote a code that uses setAnimationStyle in a popup window, when i check the code on a Galaxy 1 device running Android 2.2 OS everything worked great. But when i try it on the Galaxy 2 device running Android 2.3.5 OS the animation doen't work any more.
here is the code .
Thanks.
this is animation definition in style.xml
<style name="AnimationPopup" parent="android:Animation">
<item name="#android:windowEnterAnimation">#anim/popup_menu</item>
<item name="#android:windowExitAnimation">#anim/fadeout</item>
</style>
this is the animation located at res/anim
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%"
android:toYDelta="0"
android:duration="300"
/>
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500"/>
</set>
this is the code i use in the activity :
_menu = new PopupWindow(googlePopupInflator);
_menu.setOutsideTouchable(true);
_menu.setAnimationStyle(R.style.AnimationPopup);
_menu.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
I encountered same problem on moving to 2.3.4
Made a workaround: Animate the controls inside the PopupWindow.
My PopupWindow contained a main linear layou and inside it child linera layouts.
I tried to animate the main linear layout - didn't succeed.
Then I tried animating the child layouts and succeeded. Applied to each one of them a translate animation (a different instance of the animation to each one) and I animated them right after showing the PopupWindow. They all slide in them same time - net effect - as if the PopupWindow was animated by itself.

android imageview onClick animation

I guess this is kind of an odd question but I have tried setting onClicklistener on an ImageView and it has worked. But the problem is that the user cannot sense the click. I mean if some of u have worked on other mobile environs(like Apple iPhone) then when we click on an Image in other environs then it gives an effect on the image so that the user can understand that the image has been clicked.
I have tried setting alpha using setalpha method but it doesn't work. Though the same thing is working fine on onFocusListener implementation. Can some1 suggest a different way to modify the image on click...
I am new to android so haven't learnt the nuances of simple animation also... if there is any simple animation I can use for the same then please let me know.
Thanks!
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.5"
android:duration = "300">
</alpha>
<scale
android:fromXScale = "1"
android:toXScale = "0.9"
android:fromYScale = "1"
android:toYScale = "0.9"
android:pivotX="50%"
android:pivotY="50%"
android:duration = "50">
</scale>
</set>
I don't know if this is the correct method but defining an animation as mentioned did the trick. Now we just have to give
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(Context, R.anim.image_click));
//Your other coding if present
}
in the OnClick method and the change will be visible...
You'll want to use a drawable that contains different images for the different states you want to support. Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/img_pressed" />
<item android:state_focused="true" android:drawable="#drawable/img_focused" />
<item android:drawable="#drawable/img_at_rest" />
</selector>
Name this file img.xml or something and put it in your drawable directory, then set your ImageView's image to img.xml. #drawable/img_at_rest is the original image you're trying to use, while #drawable/img_pressed and #drawable/img_focused are the images to use for their respective states. You can also use solid colors instead of images if it's appropriate for your use case.
anim/anim_item.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."
android:duration="1000">
</alpha>
</set>
And add:
myView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.anim_item));
Not sure if it works, but have you tried setSelected()
http://developer.android.com/reference/android/widget/ImageView.html#setSelected(boolean)

Categories

Resources