0

I am trying out a simple fragment code demo ,where i replacing the fragment on orientation change

CODE:

FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

        /**
         * Check the device orientation and act accordingly
         */
        if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            /**
             * Landscape mode of the device
             */             

            LS_Fragment ls_fragment = new LS_Fragment();
            fragmentTransaction.replace(android.R.id.content, ls_fragment);
        } else {
            /**
             * Portrait mode of the device
             */
                PM_Fragment pm_fragment = new PM_Fragment();
            fragmentTransaction.replace(android.R.id.content, pm_fragment);
        }
        fragmentTransaction.commit();

Now after searching the net i found that i have to use

fragmentTransaction.setCustomAnimations()

but i am not able to understand how to write the animations xml and give it to the .setCustomAnimations() function

usr30911
  • 2,731
  • 7
  • 26
  • 56

2 Answers2

1

You just passed your In and Out Animation as arguments to .setCustomAnimations(in,out)

fragmentTransaction.setCustomAnimations(R.anim.slide_in_left2, R.anim.slide_out_right2);

Go to this fragments-translate-animation for demo or

Go to this android-switch-fragment-with-animation.html for demo

and also take a look at this Android Fragment Animation SO Post

Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
  • i wanted to know what to write in the contents of R.anim.slide_in_left2 and R.anim.slide_out_right2 given my fragment transition is based on orientation change – usr30911 Jul 08 '14 at 07:35
  • @M D I tried the following but the animations arnt working http://pastebin.com/0yHvqeT8 – usr30911 Jul 10 '14 at 11:55
0

To animate the transition between fragments you use the Fragment Manager to create a Fragment Transaction.

Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).

The following code shows how you would replace a fragment by sliding out one fragment and sliding the other one in it's place.

FragmentTransaction ft = getFragmentManager().beginTransaction();

// here you have to set your animation xml for in and out transition 
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

DetailsFragment newFragment = new DetailsFragment();

ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");

// Start the animated transition.
ft.commit();

For reference, the XML animation definitions would use the objectAnimator tag. An example of slide_in_left might look something like this:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="700"
        android:fromXDelta="-100%"
        android:toXDelta="0%" >
    </translate>
</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <translate
       android:duration="700"
       android:fromXDelta="0%"
       android:toXDelta="100%" >
   </translate>
</set>
Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41