Android FragmentTransaction Custom Animation (Unknown Animator Name: Translate)

AndroidAndroid FragmentsAndroid Animation

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSalicBlu3View Question on Stackoverflow
Solution 1 - AndroidminivacView Answer on Stackoverflow
Solution 2 - AndroidAmmar aliView Answer on Stackoverflow
Solution 3 - AndroidtxedoView Answer on Stackoverflow