46

I'm using the v4 android compatibility library to develop a tablet UI using fragments specifically for Android 2.2 devices and up.

Everything is working as it should, except that I can't get any animations to work, not even the standard animations.

Code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ABCFragment abcFragment = new ABCFragment();
    ft.replace(R.id.main_frame_layout_fragment_holder,abcFragment);     
    ft.addToBackStack(null);
    ft.commit();

Instead of using a transit animation, the fragment freezes for about a second and the just disappears and the new one appears.

Using:

ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right);

doesn't work either.

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.synergygb.mycustomapp"
android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:gravity="bottom">
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/main_frame_layout_fragment_holder">
</FrameLayout>
<!-- OTHER FIXED UI ELEMENTS-->
</RelativeLayout>

I read that the custom animation were broken in the compatibility library, but no one seems to be having issues with the standard transitions. I've tested this on a 3.2.1 Motorola Xoom, 2.3 Galaxy Tab 7", 2.2 emulator, and even on a HTC G2 with 2.3.4.

What could be wrong here?

5
  • Is dalvik (debugger) giving you anything? Oct 20, 2011 at 14:45
  • Do you have any logcat output to give us? Oct 20, 2011 at 18:23
  • Nothing, thats why im lost. No errors. Logcat shows nothing. Just regular callbacks of the system, nothing related to the in-app changes going on. Checking the value of mTransition it gives = 4099. This value is set by: ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    – blindstuff
    Oct 21, 2011 at 23:06
  • I setup a whole dev enviornment on another computer, same result.
    – blindstuff
    Oct 21, 2011 at 23:07
  • Just updated the compatibility package, same result. Im building on the 2.2 Google API.
    – blindstuff
    Oct 24, 2011 at 2:22

6 Answers 6

38

I finally got this to work after much trial and error.

First and foremost, get the lastest ACL, it did fix custom animations, and while this was not my exact problem, once those worked I ended up using them instead of standard transitions.

Right now I am using:

ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out);

The key to making it work on both Android 2.1, 2.2 and 2.3, as well as Android 3.0+ was to do the following:

  • Make sure you are using ONLY API´s available to the lowest API LEVEL you wish to support (in my case 2.1).
  • Compile using Android 3.0.
  • In the manifest file, set android:hardwareAccelerated="true" inside your application tag.

Fragment animations now work on all devices. If you do not set the extra info in the application tag, the animation will occur, but in a very very choppy way, making it seem as though it did not happen at all.

Hope this helps someone in the future!

As a note, there are some API checking tools so you are sure you are not using any APIs that aren't available to you. I prefer to work on 2.1 so the IDE doesn't show anything I can't use, once I have stable code I jump back to compiling on 3.0

3
  • Thank you for this answer, im testing on an SII, HTC evo, Nexus Galaxy and an SIII and was horrible to see the Nexus lagging the animations, with the hardware tag everything behaves as expected :)
    – Goofyahead
    Jun 7, 2012 at 14:37
  • @blindstuff If you're not using any code in an API level above the lowest you are supporting, there is no need to compile against a newer API. The only thing compiling against a newer API gives you is access to new classes and methods. Aug 23, 2012 at 16:54
  • 2
    You need to compile in 3.0 or above to have android:hardwareAccelerated="true" recognized.
    – blindstuff
    Aug 23, 2012 at 22:46
35

Try getting the most recent ACL again, they have fixed it: http://code.google.com/p/android/issues/detail?id=15623#c19

Also I noticed that for setCustomAnimations, it needs to be set before transaction calls like replace in order to take effect.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.in_from_left, R.anim.out_to_right, R.anim.in_from_right, R.anim.out_to_left);
ft.replace(android.R.id.content, newFrag);
ft.addToBackStack(null);
ft.commit();
6
  • I have done both things you describe, I did state that in the comments as to give follow up to my question. I got it to work already, but im just testing before I give an answer that im not 100% sure. I got it to work with the Galaxy Tab, on the XOOM the animations look like crap.
    – blindstuff
    Oct 25, 2011 at 3:49
  • sorry, I couldn't comment on other people's post (not enough reputation). Glad to hear you solved it though!
    – dvd
    Oct 25, 2011 at 16:46
  • 11
    This solved it for me. This mysterious "call-before" dependency is so insanely stupid, if you can't do a proper Builder-pattern then don't do it at all. Dec 14, 2012 at 0:00
  • 1
    The "call-before" is there because a transaction can have multiple operations each with different animations. Sep 24, 2014 at 21:04
  • 1
    @mark.kedzierski which makes the API inconsistent with the Builder pattern. There should be a separate builder for operations and their attributes, eg. add fragment + animation, replace fragment + animation. Which are then added to the overall fragment transaction.
    – dvd
    Aug 31, 2015 at 17:27
5

You must call setCustomAnimations before you add the fragment. This allows adding multiple fragments with different animations.

1
  • Thank you @greg-ennis I was close to madness
    – J.C
    Apr 6, 2020 at 10:18
4

to perform top_to_bottom animation for fragment,

follow same to do top to bottom

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.top_to_bottom_fragment,
android.R.animator.fade_out); ft.replace(R.id.simple_fragment,
fragment); 
ft.commit();

top_to_bottom_fragment.xml

<objectAnimator android:duration="400" android:valueFrom="-800"
    android:valueTo="0" android:propertyName="y"
    android:valueType="floatType"
    xmlns:android="http://schemas.android.com/apk/res/android" />

where valueFrom="-800" indicate bottom of your fragment layout.

3

I've added NineOldAndroids support to the Google Support library. See http://www.github.com/kedzie/Support_v4_NineOldAndroids for details. It allows using Property Animations for Fragment Transitions, PageTransformers, and some other stuff.

0

Hope this helps someone. The API docs say use objectAnimator for fragment animations, but even with latest compatibility package objectAnimator in xml wasn't accepted by compiler.

This works for me:

For Android 3.0 or higher: declare xml objectAnimator in res/animator folder.

With Compatibility package for less than 3.0: declare xml animation in res/anim folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.