0

I am trying to set animation for a relative layout in one of my activity_main.xml layout. I want the relative layout to slide upward when visibility is set to visible, and slide downward when visibility is set to gone/invisible. But I was only able to get the animation to slide upward and slide downward once. When I clicked the button again to have the layout slide upward again (and set to visible), the layout just popped out without animation; and as well as when I clicked the button again to have it slide downward (and set to gone/invisible), the layout also just disappears without animation. I would really appreciate your help. Thank you very much!

Here is what I have tried:

Private RelativeLayout relLayoutInfo = findViewById(R.id.rel_Layout_Info);
Private Button btnSlideUp = findViewById(R.id.btn_Up);
Private Button brnSlideDown = findViewById(R.id.btn_Down);
private Animation extraInfoAnimationEnter = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.details_info_animation_enter);
Private extraInfoAnimationExit = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.details_info_animation_exit);

btnSlideUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relLayoutInfo.setAnimation(extraInfoAnimationEnter);
                relLayoutInfo.setVisibility(View.VISIBLE);
            }
        });
btnSlideDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relLayoutInfo.setAnimation(extraInfoAnimationExit);
                relLayoutInfo.setVisibility(View.GONE);
            }
        });

This is the animation file for extraInfoAnimationEnter:

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

    <translate
        android:fromYDelta="100%"
        android:toYDelta="0"
        android:duration="500">

    </translate>
</set>

This is the animation file for extraInfoAnimationExit:

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

    <translate
        android:fromYDelta="0"
        android:toYDelta="100%"
        android:duration="500">

    </translate>

</set>
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Use transition api for visibility change animations. Check this answer for more details https://stackoverflow.com/a/56144349/1372866 – ashakirov Jul 15 '21 at 21:45

1 Answers1

0

This might not be the exact solution but only a suggestion, there is a MotionLayout also available here

And also playlist by official channel of AndroidDevs here

MotionLayout is great for animations, but if this isn't what you want we can try to implement it in your RelativeLayout :)

mehul bisht
  • 682
  • 5
  • 15