Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change DialogFragment enter/exit transition at just before dismissing

I have a DialogFragment and I set animation for enter/exit in the onActivityCreated method as below:

  @Override     public void onActivityCreated(Bundle arg0) {         super.onActivityCreated(arg0);         getDialog().getWindow()                 .getAttributes().windowAnimations = R.style.DialogAnimation;     } 

My DialogAnimation style files is as follows:

<style name="DialogAnimation">         <item name="android:windowEnterAnimation">@android:anim/fade_in</item>         <item name="android:windowExitAnimation">@android:anim/fade_out</item>     </style> 

This works for me now...

Now my problem is i want to have two different exit animation one for when the "OK" button is clicked and one for the cancel button. So what I did was I tried changing the transition just before dismissing but it didn't work.

Any solution on how it can be achieved?

This is what I tried:

  @Override     public void onClick(View v) {         getDialog().getWindow()                 .getAttributes().windowAnimations = R.style.DialogAnimation2;         this.dismiss();     } 
like image 616
Dominic D'Souza Avatar asked Oct 02 '15 13:10

Dominic D'Souza


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do you dismiss a DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.

How do I know if DialogFragment is showing?

that should mean its in the foreground displaying if im not mistaken. If you call DialogFragment's creation several times in one moment, dialogFragment = getSupportFragmentManager(). findFragmentByTag("dialog"); will return null, and all dialogs will be shown.


2 Answers

You can do it inside your DialogFragment, without changing

getDialog().getWindow()             .getAttributes().windowAnimations 

You should animate "decor view" in onStart and onClick.

This is the code snipped :

Create Dialog first

@Override     public Dialog onCreateDialog(Bundle savedInstanceState) {         return new AlertDialog.Builder(getActivity())                 .setTitle("Hello from animated dialog :)")                 .setNegativeButton("Cancel",                         new DialogInterface.OnClickListener() {                             public void onClick(DialogInterface dialog, int whichButton) {                                 //we have to add button here and then override it's click in onStart                             }                         }                 )                 .setCancelable(false)                 .create();     } 

Then override onStart method

@Override     public void onStart() {         super.onStart();          AlertDialog dialog = (AlertDialog)getDialog();          final View decorView = getDialog()                 .getWindow()                 .getDecorView();          ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,                 PropertyValuesHolder.ofFloat("scaleX", 0.0f, 1.0f),                 PropertyValuesHolder.ofFloat("scaleY", 0.0f, 1.0f),                 PropertyValuesHolder.ofFloat("alpha", 0.0f, 1.0f));         scaleDown.setDuration(2000);         scaleDown.start();           Button positiveButton = dialog.getButton(Dialog.BUTTON_NEGATIVE);         positiveButton.setOnClickListener(new View.OnClickListener()         {             @Override             public void onClick(View v)             {                 final View decorView = getDialog()                         .getWindow()                         .getDecorView();                  ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,                         PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),                         PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),                         PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));                 scaleDown.addListener(new Animator.AnimatorListener() {                     @Override                     public void onAnimationEnd(Animator animation) {                         dismiss();                     }                      @Override                     public void onAnimationStart(Animator animation) {                     }                     @Override                     public void onAnimationCancel(Animator animation) {                     }                     @Override                     public void onAnimationRepeat(Animator animation) {                     }                 });                 scaleDown.setDuration(2000);                 scaleDown.start();             }         });     } 

Here is the result animation

Demo of the result


And if you remove scale properties from my code you will get only alpha animation. Exactly as you wanted.

Remove this:

PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f), PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f), 
like image 194
Danylo Volokh Avatar answered Sep 24 '22 07:09

Danylo Volokh


You can set an Up & Down animations for dialog fragment. In 'res/anim' add two files:

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

// Slide dowm 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> 

// Style

<style name="DialogAnimation">     <item name="android:windowEnterAnimation">@anim/slide_up</item>     <item name="android:windowExitAnimation">@anim/slide_down</item> </style> 

// Inside Dialog Fragment

@Override public void onActivityCreated(Bundle arg0) {     super.onActivityCreated(arg0);     getDialog().getWindow()    .getAttributes().windowAnimations = R.style.DialogAnimation; } 
like image 34
Akhil Jayakumar Avatar answered Sep 23 '22 07:09

Akhil Jayakumar