71

I'm trying to get a custom animation to work with my fragment.

I've followed the online tutorials but I've been getting the below error:

java.lang.RuntimeException: Unknown animator name: translate

The XML for the animation is below:

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

The Java file is shown below:

public void goCategory(View v) {        
    FragmentTransaction ft = fm.beginTransaction();     
    ft.setCustomAnimations(R.animator.anim_in_left, R.animator.anim_out_left);              
    ft.show(fragment);
    ft.commit();
}

I'm having trouble understanding the solutions in the other threads. If someone could dumb it down for me, I'd really appreciate it.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
SalicBlu3
  • 1,874
  • 2
  • 19
  • 32
  • If you're using the animation with the support library version of Fragment, then that might be your problem. – type-a1pha Jul 20 '13 at 08:35

3 Answers3

117

Probably you are mixing two apis. There are two cases:

  • If targeting below 3.0 or using support v4 fragments: You have to use the old animations api, that is, the one you are using (they go into anim/, and are R.anim.thing)

  • If you are targeting above 3.0 and using native fragments: You have to use the new animation apis, that is, ObjectAnimators (they go into animator/ and are R.animator.thing).

minivac
  • 961
  • 1
  • 7
  • 9
  • 1
    Would you happen to know where in the documentation does it say you have to use object animators for native fragments? – kmdupr33 Aug 06 '14 at 13:04
  • 1
    I checked the source code of both the Api and find that all the animation xml should go into res/anim folder, didn't see any parsing to animator/ – Lei Guo Dec 09 '14 at 08:56
116

It will not work, you should use object animator

animator/slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="1000"
        android:valueTo="0"
        android:valueType="floatType" />

</set>

animator/slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="-1000"
        android:valueType="floatType" />

</set>

Class Subcategory

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            // return super.onCreateView(inflater, container, savedInstanceState);

            View view = (ViewGroup) inflater.inflate(R.layout.product_frame, null);
            getFragmentManager().beginTransaction()
                    .replace(R.id.sub_header, new Sub_Header()).commit();
            getFragmentManager()
                    .beginTransaction()
                    .setCustomAnimations(R.animator.slide_in_left,
                            R.animator.slide_out_right, 0, 0)
                    .replace(R.id.product_frame, new Sub_Catagory_Grid()).commit();

            view.getWidth();
            return view;

        }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Ammar ali
  • 1,503
  • 1
  • 15
  • 24
  • what's the problem post it here i dont understand your question what do u mean by its not work ? – Ammar ali Jul 02 '14 at 02:51
  • 2
    He's referring to your statement that the OP's solution won't work, and asking for clarification. – Tom Aug 14 '14 at 05:13
  • 6
    To the person who don't know why "translate animation" does not work for new Api: by reading the source code(Api 19), it only support "objectAnimator" and "animator". – Lei Guo Dec 09 '14 at 09:08
  • 4
    These animations do not provide good effects because they use hard coded translation values. I experienced overlapping one fragment with the other for instance.. I assume different screen sizes would produce different behaviour.. Unfortunately, it is not possible to specify relative translation with objectAnimators. – Sandra Nov 20 '15 at 10:03
  • shouldn't "from" value of slide_out_right be 1000 and the value of slide_in_left -1000? – muilpp Oct 21 '16 at 08:49
  • @muilpp This confuses a lot of people but the idea is if it's slide_out_right then it's sliding from the right (to the left side) not sliding out to the right side. – Itay Feldman Jan 27 '21 at 08:47
  • i have been search far too long for a solution and this was finally it, thank you so much! – mr nooby noob Jun 05 '21 at 18:48
0

As @minivac replied you are mixing two APIs. Please, take a look to Display Card Flip Animations example from Android training guides to get a further understanding about how to add custom animations to fragment transactions. It solves exactly your issue.

txedo
  • 956
  • 11
  • 11