Transition android fragment slide up

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

  • Android, class not found from imported jar file
  • Does/Will Google support NDK C++ crashes in Firebase Crash Reporting?
  • Check if correct Google Play Service available: “Unfortunately application has stopped working”
  • android autofit mode causing issues with css width in web page
  • Mute a playing video by VideoView in Android Application
  • Is there a reason to use the Observer pattern over a Broadcast Receiver?
  • <?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?

    Related posts:

    What are the Android SDK build-tools, platform-tools and tools? And which version should be used?
    Crop image in android
    DuplicateFileException m4b + play services 9.0
    Android: ProgressDialog.show() crashes with getApplicationContext
    How to enable multidexing with the new Android Multidex support library
    Will this method be called? (Still needs an accepted answer&hellip;See details in answer)
  • Exception in thread “main” java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
  • Android screen sizes in Pixels for ldpi, mdpi, hpdi?
  • Bluetooth connection issues with IntentService
  • Accessing localhost of PC from USB connected Android mobile device
  • MediaController Positioning
  • How to make my project compatible with different sdk versions while using features for an specific version
  • 5 Solutions collect form web for “Transition android fragment slide up”

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

    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.

    code for slide_in_up :

    <?xml version="1.0" encoding="utf-8"?>
        <set xmlns:android="http://schemas.android.com/apk/res/android">
            <translate xmlns:android="http://schemas.android.com/apk/res/android"
                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 xmlns:android="http://schemas.android.com/apk/res/android"
               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();
    

    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);
    

    here is complete working example

    Pressing the button toggles between 2 fragments A and B (by slide animation right to left). The fragments are just stupid text (AAAAAA and BBBBB) with different backgrounds.

    MainActivity.java

    package com.example.slidetrans;
    
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    
    public class MainActivity extends Activity {
    
        boolean showingA = true;
        Button button;
    
        A a;
        B b;
    
        private void incarnate(FragmentManager fm){
            int layoutId = R.id.frame;
            boolean fragmentWasNull = false;
            Fragment f = fm.findFragmentById(layoutId);
            if (f == null){
                if (showingA){
                    f = a = new A();
                } else {
                    f = b = new B();
                }
                fragmentWasNull = true;
            }
            if (fragmentWasNull){
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(layoutId, showingA ? a : b,  "main").commit(); 
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            FragmentManager fm = getFragmentManager();
            incarnate(fm);
            button = (Button)findViewById(R.id.button);
            OnClickListener listener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction transaction = fm.beginTransaction();
                    transaction.setCustomAnimations(R.anim.in, R.anim.out);
                    transaction.replace(R.id.frame, showingA ? new B() : new A()).commit();
                    showingA = !showingA;
                    button.setText(showingA ? "slide in B" : "slide in A");
                }
            };
            button.setOnClickListener(listener);
        }
    }
    

    LL.java

    package com.example.slidetrans;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.LinearLayout;
    
    public class LL extends LinearLayout {
    
        public LL(Context context) {
            super(context);
        }
    
        public LL(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public float getXFraction() {
            final int width = getWidth();
            if (width != 0) return getX() / getWidth();
            else return getX();
        }
    
        public void setXFraction(float xFraction) {
            final int width = getWidth();
            float newWidth = (width > 0) ? (xFraction * width) : -9999;
            setX(newWidth);
        }
    }
    

    main.xml (layout)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
    >
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="slide in B" />
    
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
        </FrameLayout>
    
    </LinearLayout>
    

    a.xml (layout)

    <?xml version="1.0" encoding="utf-8"?>
    <com.example.slidetrans.LL xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#00FF00"
    >
    
        <TextView
            android:id="@+id/aText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AAAAAAAAAAAAAAAAAA"
            android:textSize="30sp"
            android:textStyle="bold"
        />
    
    </com.example.slidetrans.LL>
    

    b.xml (layout)

    <?xml version="1.0" encoding="utf-8"?>
    <com.example.slidetrans.LL xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#FFFF00"
    >
    
        <TextView
            android:id="@+id/bText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textStyle="bold"
            android:text="BBBBBBBBBB"
        />
    
    </com.example.slidetrans.LL>
    

    in.xml (anim)

    <?xml version="1.0" encoding="utf-8"?>
    <set  xmlns:android="http://schemas.android.com/apk/res/android">
        <objectAnimator
        android:duration="500"
        android:interpolator="@android:anim/linear_interpolator"
        android:propertyName="xFraction"
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:valueType="floatType" />
    
    </set>
    

    out.xml (anim)

    <?xml version="1.0" encoding="utf-8"?>
    <set  xmlns:android="http://schemas.android.com/apk/res/android">
        <objectAnimator
        android:duration="500"
        android:interpolator="@android:anim/linear_interpolator"
        android:propertyName="xFraction"
        android:valueFrom="0.0"
        android:valueTo="-1.0"
        android:valueType="floatType" />
    
    </set>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.