How do I keep animated components from "bouncing back" to their original positions after animation?

  android, android-animation, android-studio, java

I want to make a CardView placed at the bottom of the window slide down when you switch modes, and slide back up when you go back into normal mode. The problem is that while the animation works fine, the card immediately reappears in the same spot it was in after the animation finishes. How do I get it to stay/freeze until I want to make it come back?

Here’s my animation code for hiding the card (in card_hide_ani.xml):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="500" />
</set>

…and the method that hides the card in MainActivity.java (called from button tap)

    void HideCard(Context context) {
        CardView cardView = findViewById(R.id.cardView);
        Animation cardAni = AnimationUtils.loadAnimation(context, R.anim.card_hide_ani);
        cardView.startAnimation(cardAni);
    }

(I’d also like to animate the card’s transparency/alpha, although I’m not entirely sure if that’ll work for both the card and it’s embedded components. I’ll deal with that later, but right now I just want to get this working.)

Like I said, the animation part works fine, but the card "bounces" right back to where it was. I can’t find anything that indicates why, and so far it’s been frustrating. I’m assuming that I should have done it some other way, but I can’t figure out what to do or how to go about doing it. Am I missing something here? Any help would be greatly appreciated. Thanks!

App running on Android Emulator (Android 10 (Q) on Windows 10)
Visual demonstration of problem

Source: Android Questions

LEAVE A COMMENT