Animation not working the in custom dialog
Asked Answered
B

3

8

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>
Boulanger answered 29/10, 2015 at 13:46 Comment(4)
Where are you setting your animationStyle?Braunschweig
this.getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnim; in constructorBoulanger
Are you sure you didn't mix up two animation resources (names would suggest so...)? Show us your anim resources.Switzerland
@BartoszLipinski question editedBoulanger
S
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)

Switzerland answered 29/10, 2015 at 14:0 Comment(5)
Did you try everything in that I posted in the answer?Switzerland
but its very speedy ? how to control its speedBoulanger
Change the android:duration parameter in your anim resources. You can use milliseconds there. So for example instead of this: android:duration="@android:integer/config_mediumAnimTime" you can use this android:duration="600"Switzerland
android:duration="@android:integer/config_mediumAnimTime" what do you think is this timing ?Boulanger
You should be able to check it by clicking config_mediumAnimTime with your mouse button while holding ctrl/command button. It's 400 milliseconds.Switzerland
H
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);

}
Hanfurd answered 4/9, 2018 at 4:54 Comment(0)
T
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);

}
Temptress answered 7/7, 2019 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.