Custom DialogFragment with various animations for different orientations

I have a custom one DialogFragmentwith custom animations to / from a set through Theme. It has different animations for portrait and landscape orientations. The problem is changing the orientation when displaying the dialog. It reproduces the animation corresponding to the orientation that the device had when creating the dialogue. Is there a way to reload the animation so that it matches the resources?

I tried to set the animation in again onConfigurationChanged, but didn't work.

CustomDialogFragment.java

public class CustomDialogFragment extends DialogFragment {
    @Override
    public int getTheme() {
        return R.style.CustomKeypadTheme;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getDialog().getWindow().setWindowAnimations(R.style.CustomAnimation_Window);
    }
}

styles.xml

<style name="CustomKeypadTheme" parent="@android:style/Theme.Panel">
    <item name="android:windowAnimationStyle">@style/CustomAnimation.Window</item>
</style>

<style name="CustomAnimation.Window" parent="@android:style/Animation.Activity">
    <item name="android:windowEnterAnimation">@anim/custom_animation_in</item>
    <item name="android:windowExitAnimation">@anim/custom_animation_out</item>
</style>

custom_animation_in.xml

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

    <translate
        android:fromXDelta="@dimen/custom_anim_deltaX"
        android:fromYDelta="@dimen/custom_anim_deltaY"
        android:toXDelta="0"
        android:toYDelta="0"
        android:duration="400" />

</set>

custom_animation_out.xml

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

    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="@dimen/custom_anim_deltaX"
        android:toYDelta="@dimen/custom_anim_deltaY"
        android:duration="400" />

</set>

custom_anim_deltaXand custom_anim_deltaYhave different meanings for portrait and landscape orientations.

+4

Source: https://habr.com/ru/post/1544316/


All Articles