Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animation on pressing back button

Tags:

android

I have added the following code to my activity and got the desired animation, but pressing the back button the animation is not the same I.e the activity just closes normally. How can I add animation when back button is pressed

public void notesAndCodeClick(View v){

    Intent notesIntent = new Intent(MainActivity.this, NotesActivity.class);
    ActivityOptions notesoptions = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), v.getHeight());
    startActivity(notesIntent, notesoptions.toBundle());
}
like image 723
boomboxboy Avatar asked Oct 30 '15 10:10

boomboxboy


3 Answers

Try this,

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        this.overridePendingTransition(R.anim.trans_right_in,
                R.anim.trans_right_out);
    }

Add both the files listed below to your anim folder

res --> anim

trans_right_in.xml

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

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="-100%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0" />

</set>

trans_right_out.xml

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

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="100%p" />

</set>
like image 131
Parth Bhayani Avatar answered Nov 18 '22 11:11

Parth Bhayani


You can set IN and OUT animation for Activity while pressing back button.

Left to Right animation :

Put this file in res/anim/left_to_right.xml :-

 <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
      <translate android:fromXDelta="-100%" android:toXDelta="0%"
                 android:fromYDelta="0%" android:toYDelta="0%"
                 android:duration="700"/>
    </set>

Right to Left animation :

Put this file in res/anim/right_to_left.xml :-

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate
     android:fromXDelta="0%" android:toXDelta="100%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="700" />
</set>

Now in onBackPressed() : -

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right);   
}
like image 37
KishuDroid Avatar answered Nov 18 '22 11:11

KishuDroid


You have to just do one thing, call animation after finishing your activity like this.

finish();
overridePendingTransition(R.anim.nothing,R.anim.nothing);

Happy Coding....

like image 2
Sanwal Singh Avatar answered Nov 18 '22 10:11

Sanwal Singh