Setting an Android fragment to enter and exit transitions?

I am trying to set input and output transitions for DialogFragment, which seems straightforward enough. Here is the code from my DrinkDetailActivity that creates the dialog:

fab.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //create and display the dialog DialogFragment orderFragment = new OrderDrinkDialogFragment(); //set the slide transition for the fragment Transition dialSlide = new Slide(); dialSlide.setDuration(300); orderFragment.setEnterTransition(dialSlide); orderFragment.setExitTransition(dialSlide); orderFragment.show(getFragmentManager(), "order"); } }); 

And here is the code from my OrderDrinkDialogFragment class:

 public class OrderDrinkDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(DrinkDetailActivity.activity); //set up the Linear Layout of the dialog LinearLayout layout = new LinearLayout(DrinkDetailActivity.activity); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout.setOrientation(LinearLayout.VERTICAL); layout.setLayoutParams(params); //set up the EditText view EditText nameField = new EditText(DrinkDetailActivity.activity); nameField.setHint(R.string.dialog_name_hint); //add the EditText view to the layout layout.addView(nameField, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); //set layout as the view of the dialog builder.setView(layout); return builder.create(); } } 

DrinkDetailActivity.activity is simply this from an instance of DrinkDetailActivity . For some reason, the dialog doesn't go when I show() it or when it is fired, it just appears and goes out of view. This tutorial helped me understand activity transitions for the rest of my application, and I followed the same instructions for fragment transitions, as he says. What am I doing wrong?

+8
android android-fragments
source share
3 answers

It worked for me. My slide 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> 

and my slide 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> 

in your styles add this

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

now finally in onCreateDialog() of the Dialog fragment add this

 public Dialog onCreateDialog(Bundle savedInstanceState) { final Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; return dialog; } 
+9
source share

You need to write a style to show the animation in any window, for example Dialog

 <style name="AnimDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowAnimationStyle">@style/DialogAnimation</item> </style> <style name="DialogAnimation"> <item name="android:windowEnterAnimation">@anim/fadein</item> <item name="android:windowExitAnimation">@anim/fadeout</item> </style> 

For your dialogue you need to create an instance, for example

 Dialog animatedDialog = new Dialog(MainActivity.this,R.style.AnimDialog); 
0
source share

DialogFragment is not only a dialog, but also a fragment ; see Basic Dialog for more details on how to use it. You used onCreateDialog inside the fragment, which may be the cause of your problem. Thus, use onCreateView to create the fragment dialog box.

Hope this answer helps you.

-one
source share

All Articles