Transition android fragment slide up

55,839

Solution 1

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

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="100%p"
    android:toYDelta="0%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="0%p"
    android:toYDelta="-100%p" />

Solution 2

It's been some time since this question was asked but here is an answer for other people coming here:

e1da is right insofar as that setCustomAnimation() call has to be called before replace(). Otherwise the animation will not show.
The second problem is that you probably are using native fragments that cannot be animated with the view animations.

Use the following files:

slide_in_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >
    <objectAnimator
        android:duration="500"
        android:propertyName="y"
        android:valueFrom="1280"
        android:valueTo="0"
        android:valueType="floatType" />
</set>

slide_out_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >
    <objectAnimator
        android:duration="500"
        android:propertyName="y"
        android:valueFrom="0"
        android:valueTo="-1280"
        android:valueType="floatType" />
</set>

A little explanation:
You have to differentiate between view animation for support fragments on one hand and property animation for native fragments on the other hand.

View animation:
Is the pre-android 3.0 way to animate views. Sample code for this is the slide_in.xml and slide_up.xml by user3093402

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

It is worth mentioning you cannot animate fragments with view animation. The exception to this are fragments from the support library (android.support.v4.app.Fragment).

Property animation
This is the way to animate objects after android 3.0. It is also declared as .xml files but makes use of the "valueAnimator" tag (objectAnimator extends valueAnimator). Examples are in the answer to the question. This is the way how native fragments (android.app.Fragment) can be animated.

See also:

Hope this helps,
Kai

EDIT: As pointed out by Raphael Royer-Rivard the fixed screen size is bad practice. It would be better to use a constant from the OS like in getWindowManager().getDefaultDisplay().getMetrics(metrics).xdpi (see DisplayMetrics). But I haven't done any Android development for some time so I don't know which one.

Solution 3

code for slide_in_up :

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

code for slide_in_down:

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

code for slide_out_up:

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

code for slide_out_down:

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

and after that in your activity or fragment set animation like below:

    Fragment fragment = new Fragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_in_down, R.anim.slide_out_down, R.anim.slide_out_up);
    transaction.replace(container, fragment).commit();

Solution 4

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

Note: Min version L.

Solution 5

R.anim will not work there but R.animator wil do. for example

transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
Share:
55,839
user3093402
Author by

user3093402

Updated on January 19, 2021

Comments

  • user3093402
    user3093402 over 3 years

    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?

  • Muhammad Babar
    Muhammad Babar over 9 years
    using view animation for support fragments. when trying to slide_out_up the fragment doesn't move out of screen but shrinks from bottom to top.
  • Raphael Royer-Rivard
    Raphael Royer-Rivard almost 9 years
    I believe using an hardcoded height value of 1280 is a very bad thing. What if the screen is bigger than that?
  • Antoniossss
    Antoniossss over 6 years
    but not given xml files
  • Antoniossss
    Antoniossss over 6 years
    It will work only with Support library. What about API >=11 without it?
  • Ultimo_m
    Ultimo_m almost 6 years
    Dont forget to use transaction.Add(..) instead of replace when you want pop animation and the setter should be before add when you write the code
  • Misagh
    Misagh almost 6 years
    It's work, but the clear answer should changes the "android:fromXDelta" to "android:fromYDelta"
  • jesses.co.tt
    jesses.co.tt almost 5 years
    Can you add a link to this API?