Transition android fragment slide up

Viewed 57024

I have a fragment that is to replace another fragment. I want to specify the animation. But the animation is ignored.

transaction.replace(R.id.my_fragment, newFrag);
transaction.addToBackStack(null);
transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up);

slide_in_up

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="100%p" />

slide_out_up

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="100%p"
    android:toYDelta="0%p" />

All I am really trying to achieve is for the new fragment to slide in from the bottom. My animations are ignored. What is the code missing?

7 Answers

Currently with android.transition this is as simple as fragment.enterTransition = Slide() or fragment.enterTransition = Fade()

Note: Min version L.

This answer is written in Kotlin. For sake of the example, I'm just hiding or showing it based on it's current isHidden attribute. I'm using androidx.transition.Slide

Link: https://developer.android.com/reference/androidx/transition/Slide

val frag = supportFragmentManager.findFragmentById(R.id.frag_id_from_xml_layout)
frag?.let {
    val ft = supportFragmentManager.beginTransaction()
    frag.enterTransition = Slide()
    frag.exitTransition = Slide()
    if (frag.isHidden) ft.show(frag) else ft.hide(frag)
    ft.commit()
}
Related