44

I am replacing a fragment with another fragment. I want the first fragment to disappear with a fade-out effect and second fragment to appear with fade-in effect. How is this done?

Lancer521
  • 81
  • 3
  • 11
Qadeer Hussain
  • 653
  • 2
  • 7
  • 17

3 Answers3

66

With addition to @MD code

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();

ft.setCustomAnimations(R.anim.fade_in,
                R.anim.fade_out);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();

and When you Pop Fragment then apply animation like:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.fade_out, R.anim.fade_in);

ft.replace(R.id.realtabcontent, fragment);      
ft.commit();

and XML for fadeIn

<set xmlns:android="http://schemas.android.com/apk/res/android">
       <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

and XML for fadeOut

<set xmlns:android="http://schemas.android.com/apk/res/android">
       <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>
Rafique Mohammed
  • 3,666
  • 2
  • 38
  • 43
  • 54
    You don't really need to create xml for base animations. Use default the ones : `setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);` – Nexen Feb 01 '16 at 03:50
  • @Nexen yeah.. but if u notice M D's comment the guy asked for xml file code. – Rafique Mohammed Feb 01 '16 at 07:03
  • 4
    Use ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out); instead of ft.setCustomAnimations(R.anim.fade_out, R.anim.fade_int);. First parameter is enter second parameter is exit. – mertsimsek Jul 28 '16 at 08:26
  • @Nexen its not true . you can't use `anim` instead of `animator` . you can only use animator defaults for Fragment Transactions like this : `(android.R.animator.fade_in, android.R.animator.fade_out);` – SAYE Mar 13 '17 at 04:47
  • use this default : `setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);` – SAYE Mar 13 '17 at 04:48
  • `setCustomAnimations` can be used with four arguments like `setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out);` to set animations when popping the back stack – marioosh May 16 '17 at 08:34
  • 1
    @Qadeer Hussain accept this as the correct answer, please – Leonardo Sibela Jun 13 '18 at 02:44
  • Note the order matters: you must call the `setCustomAnimations` before you call `add` or `replace` – hmac May 16 '19 at 20:48
14

When you Push a Fragment then apply animation like:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();

ft.setCustomAnimations(R.anim.fade_in,
                R.anim.fade_out);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();

and When you Pop Fragment then apply animation like:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.fade_out, R.anim.fade_in);

ft.replace(R.id.realtabcontent, fragment);      
ft.commit();

Hope this works for you.

Update: For more information go to

  1. http://android-er.blogspot.in/2013/04/implement-animation-in.html
  2. Animate the transition between fragments
Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
6

It is worth adding that setCustomAnimations can also have 4 arguments:

FragmentTransaction setCustomAnimations (int enter, 
                                         int exit, 
                                         int popEnter, 
                                         int popExit)

Set specific animation resources to run for the fragments that are entering and exiting in this transaction. The popEnter and popExit animations will be played for enter/exit operations specifically when popping the back stack.

Simon
  • 2,643
  • 3
  • 40
  • 61