hectichavana
hectichavana

Reputation: 1446

How to set an animation for Fragment?

I want to implement an animation for a fragment on my activity. But regular startAnimation() method isn't applicable to fragment.

How can I set an animation to my fragment? The following is my snippet:

Fragment listview = (Fragment)getSupportFragmentManager().findFragmentById(R.id.my_tab_fragment);
final Animation a = AnimationUtils.loadAnimation(this, R.anim.translate);

Upvotes: 1

Views: 1342

Answers (2)

MahdeTo
MahdeTo

Reputation: 11184

You need to use the FragmentManager to create a FragmentTransaction Normally you can have 2 animations one for the slide in of the new and one for the slide out of the old I'll use yours for both, like this:

FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.setCustomAnimations(a, a);
tx.replace(oldFragmentReference, newFragmentReference);
tx.commit();

Hope this helped.

Upvotes: 1

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72301

To animate the adding/removing of a fragment you should use :

 getFragmentManager().beginTransaction()
                            .setCustomAnimation();

You should have a look at FragmentTransaction documentation .

Just be carefull and call the setCustomAnimation() method, before add(fragment) or replace(fragment) on your FragmentTransaction.

Upvotes: 2

Related Questions