Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a fade-in/fade-out animation when replacing a fragment

Tags:

android

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?

like image 594
Qadeer Hussain Avatar asked May 02 '14 05:05

Qadeer Hussain


People also ask

How do you animate a fragment?

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).

What is Fade in Fade out effect?

The Fade In/Fade Out behavior lets you dissolve into and out of any object by ramping the opacity of the object from 0 percent to 100 percent at the start, and then back to 0 percent at the end. You can eliminate the fade-in or fade-out effect by setting the duration of the Fade In Time or Fade Out Time to 0 frames.

How do you animate fragment transitions?

At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.


1 Answers

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> 
like image 182
Rafique Mohammed Avatar answered Oct 13 '22 14:10

Rafique Mohammed