Animation not working the in custom dialog

Coas Mckey picture Coas Mckey · Oct 29, 2015 · Viewed 7.8k times · Source

I have made the custom dialog following this link and its working perfectly all fine. but then I think to add the animation , so that it looks like coming from the to of the screen to the bottom side. So I searched for these two animations and found them And I put them in the anim folder. so to apply them in my custom dialog I have changed the constructor a little bit . I have added this line in the constructor of the custom dialog

 public AnimationDialog(Activity a, int drawable) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
    this.mDrawable = drawable;
    this.getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnim;
}

the following line is what I have added to achieve the animation as shown above

this.getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnim;

but nothing happens , my dialog appear as it appears by default

and here is my style file for reference

 <style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_down_animation</item>
    <item name="android:windowExitAnimation">@anim/slide_up_animation</item>
</style>

<!-- Animation for dialog box -->
<style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>

But still my animation is not working , what I am doing wrong , ? please tell me How can I achieve this ? How can I animate my custom dialog.

Edit

this is my slide down animation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="100%p" />

</set>

this is my slide up animation

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="0" />
</set>

Answer

Bartek Lipinski picture Bartek Lipinski · Oct 29, 2015

Try this:

slide_down_animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="-100%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0%p" />
</set>

and

slide_up_animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="-100%p" />
</set>

EDIT:

Apart from that, you can also try setting your style this way:

getWindow().setWindowAnimations(R.style.DialogAnimation);

(NOT R.style.DialogSlideAnim)