Coas Mckey
Coas Mckey

Reputation: 709

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>

Upvotes: 8

Views: 11260

Answers (3)

SAWJUSTO
SAWJUSTO

Reputation: 369

Attach animation in style.xml for DialogFragment in onStart() callback function

@Override
public void onStart() {
  super.onStart();

  if (getDialog() == null) {
    return;
  }

  // set the animations to use on showing and hiding the dialog
  getDialog().getWindow().setWindowAnimations(R.style.DialogAnimation);

}

Upvotes: 2

Afjalur Rahman Rana
Afjalur Rahman Rana

Reputation: 813

Try this:

slide_down.xml

<?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"
        android:toYDelta="100%" />
</set>

slide_up.xml

<?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:toYDelta="0" />
</set>

in style.xml add this style

 <style name="DialogStyle" 
    parent="Theme.MaterialComponents.DayNight.Dialog.Bridge">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorPrimary">@color/colorPrimary</item>

    <!-- Additionally if you want animations when dialog opening -->
    <item name="android:windowEnterAnimation">@anim/slide_up</item>
    <item name="android:windowExitAnimation">@anim/slide_down</item>
</style>

then in on the start of the dialog add this

 @Override
public void onStart() {
    super.onStart();
    if (getDialog() == null||getDialog().getWindow() == null) {
        return;
    }
    getDialog().getWindow().setWindowAnimations(R.style.DialogStyle);

}

Upvotes: 1

Bartek Lipinski
Bartek Lipinski

Reputation: 31438

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)

Upvotes: 7

Related Questions