Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Android fragment enter and exit transitions?

I am trying to set enter and exit transitions for a DialogFragment, which seems straight forward 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 within the instance of DrinkDetailActivity. For some reason, the dialog doesn't transition when i show() it or when it is dismissed, it simply pops in and out of view. This tutorial helped me a lot in figuring out the Activity transitions for the rest of my app, and I followed the same instructions for fragment transitions as it says. What am I doing wrong?

like image 697
mithunm93 Avatar asked Feb 11 '15 08:02

mithunm93


1 Answers

This worked for me.My slide up animation is

<?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 is

<?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 your onCreateDialog() of Dialog fragment add this

public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
    return dialog;
}
like image 148
williamj949 Avatar answered Sep 22 '22 16:09

williamj949