philomath
philomath

Reputation: 2209

Loading fragment didn't fade in or fade out

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);
    }
}

Upvotes: 2

Views: 3838

Answers (3)

Carsten
Carsten

Reputation: 806

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

Upvotes: 1

Satender Kumar
Satender Kumar

Reputation: 635

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();

Upvotes: 1

Danial Hussain
Danial Hussain

Reputation: 2530

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);
}

Upvotes: 0

Related Questions