0

I want to hide a layout on click button with right to left animation. I have already added animation to same layout while appearing the view. Now I want to hide that view with animation.

 //on appearing view
 Animation anim = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
 mylayout.startAnimation(anim1); 

Now I want to hide same layout with rigth to left animation. And then I want to set visibility GONE.

Pro Mode
  • 1,453
  • 17
  • 30
pradip_android
  • 283
  • 1
  • 3
  • 16

3 Answers3

2

You can make a animation resource and using on your startActivity

Activity

Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
overridePendingTransition(R.anim.right_left_in, R.anim.right_left_out);
finish();

animation res----> ../anim/right_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
    <translate 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:duration="300"
          android:fromXDelta="100%p" 
          android:toXDelta="0%p">
    </translate>

animation res----> ../anim/right_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<translate 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="300"
      android:fromXDelta="0%p" 
      android:toXDelta="100%p">
</translate>

==========================

OTHERS ANIMATION

../anim/move_left_in_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<translate 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="300"
      android:fromXDelta="-100%p" 
      android:toXDelta="0%p">
</translate>

../anim/move_left_out_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<translate 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="300"
      android:fromXDelta="0%p" 
      android:toXDelta="-100%p">
</translate>

../anim/slid_in.xml

<?xml version="1.0" encoding="utf-8"?>
    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@android:integer/config_longAnimTime"
        android:fromXDelta="100%p"
        android:toXDelta="0%p">
    </translate>

../anim/slid_out.xml

<?xml version="1.0" encoding="utf-8"?>
    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@android:integer/config_longAnimTime"
        android:fromXDelta="0%p"
        android:toXDelta="-100%p">
    </translate>

../anim/zoom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" >
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="3"
        android:toYScale="3" >
    </scale>
</set>
Aspicas
  • 4,498
  • 4
  • 30
  • 53
2

You can make same XML for animation Right to left and then apply animation same as you did and add animation listener like below, and just set Visibility gone to your view in onAnimationEnd

 anim .setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
Akshay Panchal
  • 695
  • 5
  • 15
0

Create "anim" Android resource directory below res folder : enter image description here

Then create new anim file called slide_left_out.xml and write this code :

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together"
    android:duration="350">
    <translate
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        android:fromYDelta="0"
        android:toYDelta="50%p"/>
    <scale
        android:fromXScale="1"
        android:toXScale="0.5"
        android:fromYScale="1"
        android:toYScale="0.5"/>
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0"/>
</set>

Then apply the animation to the view

Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left_out);
mylayout.startAnimation(anim);
zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46