7

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>
Community
  • 1
  • 1
Coas Mckey
  • 701
  • 1
  • 13
  • 39

3 Answers3

7

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)

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
2

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

}
SAWJUSTO
  • 369
  • 1
  • 5
  • 19
1

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

}