Android How-To: Card Flip Animation in API 23

Reading Time: < 1 minute

Hello!

Lots of android devs are using this tutorial from the Google documentation to try to achieve a card flip animation, and they’re getting errors, including this one:

 05-22 11:32:34.706: E/AndroidRuntime(6801): java.lang.RuntimeException: Unknown animation name: objectAnimator

There does not appear to be a definitive answer that I can find as to how to get this to work. I got it. This is the relevant portion of my build file:

repositories{
flatDir{
dirs 'libs'
}
}
dependencies {
//Design and Aminations
compile 'com.android.support:cardview-v7:23.1.1'
compile (name:'library-2.4.1', ext:'aar')
compile 'com.android.support:support-v13:21.0.+'
compile 'com.nineoldandroids:library:2.4.0'
...
}

You’ll notice that I mention this library in the build. I downloaded it, rather than access it on bintray, to hedge against changes in its availability, and put it in the build/libs directory of my app.

When I go to flip in/out the child fragments, my parent fragment has this click listener on my flip button:

flipButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mCardSideShowing.equals(CardSide.FRONT)) {
getActivity().getFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out)
.replace(R.id.card_fragment_container, new CardBackFragment())
.commit();
mCardSideShowing = CardSide.BACK;
} else if (mCardSideShowing.equals(CardSide.BACK)) {
getActivity().getFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out)
.replace(R.id.card_fragment_container, new CardFrontFragment())
.commit();
mCardSideShowing = CardSide.FRONT;
}
}
}
});

If you’re been banging your head against this (like I did at first), please pay it forward and share this with another Android dev who might like to know.

card flip

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.