1

I'm applying transition set for entering fragment and for exiting i want to apply slide out animation, how can i do that, here is my code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    productDetailFragment.setSharedElementEnterTransition(new DetailsTransition());
    productDetailFragment.setEnterTransition(new Fade());
    productDetailFragment.setExitTransition(new Slide(Gravity.RIGHT));
}
((MainActivity)context).getSupportFragmentManager()
                       .beginTransaction()
                       .replace(R.id.fragment, productDetailFragment)
                       .addSharedElement(viewHolder.iv, "kittenImage")
                       .addToBackStack(null)
                       .commit();
ColdFire
  • 6,764
  • 6
  • 35
  • 51
blackHawk
  • 6,047
  • 13
  • 57
  • 100

3 Answers3

0

Call Fragment :

FragmentManager fragmentManager = getSupportFragmentManager();
boolean fragmentPopped = fragmentManager.popBackStackImmediate("OtpFragment", 0);
Fragment fragment = null;
Bundle b = new Bundle();

if (!fragmentPopped && fragmentManager.findFragmentByTag("OtpFragment") == null) {
    fragment = new OtpFragment();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_from_left);
    transaction.replace(R.id.frame_container, fragment, "OtpFragment")
               .addToBackStack("OtpFragment")
               .commit();
}

Create anim directory inside res folder : now create two separate files for enter/exit animation : i.e. R.anim.enter_from_right and R.anim.exit_from_left

Enter Slide Animation :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
    android:fromXDelta="100%" android:toXDelta="0%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="300" />
</set>

Exit Side animation :

<?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%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="300"/>
</set>
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Manish Gupta
  • 129
  • 9
0

Please check below code for custom animation apply to fragment transitions:

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"/>

Above i am showing you slide_up and down transition but you can use left-right and any custom objectAnimator to show animations into your fragment transition time and hope it helps you.

Jyubin Patel
  • 1,373
  • 7
  • 17
0

try something like this.! may this work fine for you.! Fragment Transaction Example

Using Slide Example

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

Fade exitFade = new Fade();
exitFade.setDuration(FADE_DEFAULT_TIME);
previousFragment.setExitTransition(exitFade);

or

Transition slideTransition = new Slide(Gravity.BOTTOM);
Transition fadeTransition = new Fade();
TransitionSet set = new TransitionSet();
set.addTransition(slideTransition);
set.addTransition(fadeTransition);
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47