2

I have a fragment that I want it to be fade in when it is added and fade out when it is removed. The way I do it at the moment is as following: (though, it doesn't have any animation effects)

Fadein when added:

FragmentManager manager =getFragmentManager();
manager.beginTransaction().setCustomAnimations(R.anim.fadein, R.anim.fadeout)
                .add(R.id.loading_fragment_container, loadingFragment, "loadingFragment").commit();

Fade out when removed:

manager.beginTransaction().setCustomAnimations(R.anim.fadeout, R.anim.fadein)
            .remove(loadingFragment).commit();

fadein.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android">
    android:interpolator="@android:anim/linear_interpolator"
    android:propertyName="alpha"
    android:valueFrom="0.0"
    android:valueTo="1"
    android:duration="1500"
</objectAnimator>

fadeout.xml

<?xml version="1.0" encoding="utf-8"?>

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android">
    android:interpolator="@android:anim/linear_interpolator"
    android:propertyName="alpha"
    android:valueFrom="1"
    android:valueTo="0.0"
    android:duration="1500"
</objectAnimator>

My fragment class:

public class LoadingFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_loading_layout, container,false);
    }

    //I even tried to add this but still nothing happens
    @Override
    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
        return super.onCreateAnimator(android.R.anim.fade_in, true, android.R.anim.fade_out);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) { 
        super.onActivityCreated(savedInstanceState);
        //basically this code just makes the loading image (which is a circle) spinning on its center forever
        //https://stackoverflow.com/questions/3137490/how-to-spin-an-android-icon-on-its-center-point
        ImageView loadingCircle= (ImageView) getActivity().findViewById(R.id.loadingImage);
        Log.d("loadingCircle","loadingCircle: "+loadingCircle);
        RotateAnimation r = new RotateAnimation(0.0f, 10.0f * 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        r.setDuration(10000);
        r.setInterpolator(new LinearInterpolator());
        r.setRepeatCount(Animation.INFINITE);
        r.setRepeatMode(Animation.INFINITE);
        r.setFillEnabled(true);
        r.setFillAfter(true);
        loadingCircle.startAnimation(r);
    }
}
Community
  • 1
  • 1
philomath
  • 2,209
  • 6
  • 33
  • 45

3 Answers3

1

Try this:

manager.beginTransaction().setCustomAnimations(R.anim.fadein, R.anim.fadeout, R.anim.fadein, R.anim.fadeout)
                .add(R.id.loading_fragment_container, loadingFragment, "loadingFragment").commit();

BTW: 1.5 sec is quite long as animation period

Qazi Fahim Farhan
  • 2,066
  • 1
  • 14
  • 26
Carsten
  • 806
  • 5
  • 6
  • yah, I just add 1.5 sec to see the animation more clearly. Btw, Your solution requires API level 13, but my current min API is 11 – philomath Oct 20 '14 at 08:01
  • You could try the Support Fragment Manager if that is an option for you because there this method works – Carsten Oct 20 '14 at 08:45
1

For Fadein FragmentManager manager =getFragmentManager(); manager.beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out) .add(R.id.loading_fragment_container, loadingFragment, "loadingFragment").commit();

For Fadeout manager.beginTransaction().setCustomAnimations(android.R.animator.fadeout, android.R.animator.fadein) .remove(loadingFragment).commit();

Satender Kumar
  • 627
  • 4
  • 7
0

In fragment override this

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    return AnimationUtils.loadAnimation(getActivity(),
            enter ? android.R.anim.fade_in : android.R.anim.fade_out);
}
Danial Hussain
  • 2,488
  • 18
  • 38
  • I tried your solution, but onCreateAnimation is never called. It's grey out by Android Studio. I also add my fragment class in my question. – philomath Oct 20 '14 at 08:07
  • why you are not placing @Override in your fragment class – Danial Hussain Oct 20 '14 at 08:10
  • and also for the time being comment your onActivityCreated code – Danial Hussain Oct 20 '14 at 08:12
  • Android Studio actually gives me an error there, it said "Method does not override method from its superclass". As I extend Fragment seems like I only can override the similar method: public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) – philomath Oct 20 '14 at 08:15
  • Little late, if you are using native `Fragment` as opposed to the support library's one, you should override `onCreateAnimator(...)` method – Sazid Nov 23 '15 at 11:01