Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display A DialogFragment with Animation

Tags:

android

I want to display the dialogfragment with specified animation as I click a button, this dialogfragment slides from left to right, and doesn't fill the screen, so when I click the area out of this dialogFragment, it will dismiss with an animation, which direction is opposite, from right to left. Is this action reasonable? If I want to complete similar action, what should I do? Any help is much appreciated.

like image 579
wislie Avatar asked Jun 06 '16 02:06

wislie


1 Answers

First define proper animation like following and put them in anim folder.

Slide_in_from_left:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:zAdjustment="top">
    <translate
        android:duration="400"
        android:fromXDelta="-100%p"
        android:toXDelta="0%p" />

    <alpha
        android:duration="400"
        android:fromAlpha="0.8"
        android:toAlpha="1.0" />
</set>

Slide_out_to_right

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:zAdjustment="bottom">
    <translate
        android:duration="400"
        android:fromXDelta="0%p"
        android:toXDelta="100%p" />

    <alpha
        android:duration="400"
        android:fromAlpha="1.0"
        android:toAlpha="0.2" />
</set>

then define custom style which defines Enter/Exit Animation:

in your style.xml :

<!-- Animations for Dialogs-->
<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_in_from_left</item>
    <item name="android:windowExitAnimation">@anim/slide_out_to_right</item>
</style>

And in your DialogFragment override onActivityCreated and assign Animation:

@Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);

        getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
    }
like image 138
Amir Avatar answered Oct 13 '22 02:10

Amir