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());
}
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>
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);
}
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With