androidandroid-animationandroid-dialogcustomdialog

Animation not working the in custom dialog


I have created a custom dialog following this link and it is working perfectly. Now I would like to add some animations to it, so that it looks like it is coming from the to of the screen to the bottom side. I've searched for and found these two animations, and I have put them in the anim folder. 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 appears as it appears by default.

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>

And still my animation is not working, what am I doing wrong?

Could you 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>

Solution

  • 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)