I've check some tutorials for animate transaction between fragments. I've used this method for animation and it works:
fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
But I want to invert this animation: old fragment slide out to the left, and new fragment slide in to the right, but no value of R.anim
file seems to be useful for my scope.
How can I do it?
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.
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).
Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class. You can group multiple actions into a single transaction—for example, a transaction can add or replace multiple fragments.
FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout , so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.
UPDATE For Android v19+ see this link via @Sandra
You can create your own animations. Place animation XML files in res > anim
enter_from_left.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="-100%p" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="@android:integer/config_mediumAnimTime"/> </set>
enter_from_right.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="100%p" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="@android:integer/config_mediumAnimTime" /> </set>
exit_to_left.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="0%" android:toXDelta="-100%p" android:fromYDelta="0%" android:toYDelta="0%" android:duration="@android:integer/config_mediumAnimTime"/> </set>
exit_to_right.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="0%" android:toXDelta="100%p" android:fromYDelta="0%" android:toYDelta="0%" android:duration="@android:integer/config_mediumAnimTime" /> </set>
you can change the duration to short animation time
android:duration="@android:integer/config_shortAnimTime"
or long animation time
android:duration="@android:integer/config_longAnimTime"
USAGE (note that the order in which you call methods on the transaction matters. Add the animation before you call .replace, .commit):
FragmentTransaction transaction = supportFragmentManager.beginTransaction(); transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right); transaction.replace(R.id.content_frame, fragment); transaction.addToBackStack(null); transaction.commit();
There is three way to transaction animation in fragment.
So need to use one of the built-in Transitions, use the setTranstion() method:
getSupportFragmentManager() .beginTransaction() .setTransition( FragmentTransaction.TRANSIT_FRAGMENT_OPEN ) .show( m_topFragment ) .commit()
You can also customize the animation by using the setCustomAnimations() method:
getSupportFragmentManager() .beginTransaction() .setCustomAnimations( R.anim.slide_up, 0, 0, R.anim.slide_down) .show( m_topFragment ) .commit()
slide_up.xml
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:propertyName="translationY" android:valueType="floatType" android:valueFrom="1280" android:valueTo="0" android:duration="@android:integer/config_mediumAnimTime"/>
slide_down.xml
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:propertyName="translationY" android:valueType="floatType" android:valueFrom="0" android:valueTo="1280" android:duration="@android:integer/config_mediumAnimTime"/>
Finally, It's also possible to kick-off multiple fragment animations in a single transaction. This allows for a pretty cool effect where one fragment is sliding up and the other slides down at the same time:
getSupportFragmentManager() .beginTransaction() .setCustomAnimations( R.anim.abc_slide_in_top, R.anim.abc_slide_out_top ) // Top Fragment Animation .show( m_topFragment ) .setCustomAnimations( R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom ) // Bottom Fragment Animation .show( m_bottomFragment ) .commit()
To more detail you can visit URL
Note:- You can check animation according to your requirement because above may be have issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With