Essential Android Fragments edit forum

Animate the transition between fragments

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the FragmentManager to create a FragmentTransaction.

For a single FragmentTransaction, there are two different ways to perform animations: you can use a standard animation or you can supply your own custom animations.

Standard animations are specified by calling FragmentTransaction.setTransition(int transit), and using one of the pre-defined constants available in the FragmentTransaction class. At the time of writing, these constants are:


The complete transaction might look something like this:

    .replace(, new MyFragment(), "MyFragmentTag")

Custom animations are specified by calling either FragmentTransaction.setCustomAnimations(int enter, int exit) or FragmentTransaction.setCustomAnimations(int enter, int exit, int popEnter, int popExit).

The enter and exit animations will be played for FragmentTransactions that do not involve popping fragments off of the back stack. The popEnter and popExit animations will be played when popping a fragment off of the back stack.

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

    .setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right)
    .replace(, new MyFragment(), "MyFragmentTag")

The XML animation definitions would use the objectAnimator tag. An example of slide_in_left.xml might look something like this:

<?xml version="1.0" encoding="utf-8"?>
  <objectAnimator xmlns:android=""

Table Of Contents
44 Fragments
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close