alexanderblom alexanderblom - 5 months ago 38
Android Question

Android Fragments and animation

How should you implement the sort of sliding that for example the Honeycomb Gmail client uses?


handle this automatically by adding and removing the Fragments, it's kind of difficult to test this due to the emulator being a slideshow :)

Answer Source

To animate the transition between fragments, or to animate the process of showing or hiding a fragment 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();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

DetailsFragment newFragment = DetailsFragment.newInstance();

ft.replace(, newFragment, "detailFragment");

// Start the animated transition.

To achieve the same thing with hiding or showing a fragment you'd simply call or ft.hide, passing in the Fragment you wish to show or hide respectively.

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"?>
  <objectAnimator xmlns:android=""
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download