I want to achieve a sliding effect from left to right when I move from one activity to another. For that I am using the following code, but I am not getting any results. Please correct me.
In java both files:
super.onCreate(savedInstanceState); overridePendingTransition(R.anim.fadein, R.anim.fadeout); setContentView(R.layout.main);
Two files in res/anim directory:
fadein.xml
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000" android:fromAlpha="0.0" android:interpolator="@android:anim/slide_out_right" android:toAlpha="1.0" > </alpha>
fadeout.xml
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000" android:fromAlpha="0.0" android:interpolator="@android:anim/slide_in_left" android:toAlpha="1.0" > </alpha>
Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.
Add this two file in res/anim folder.
R.anim.slide_out_bottom
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:duration="@integer/time_duration_max" android:fromXDelta="0%" android:fromYDelta="100%" android:toXDelta="0%" android:toYDelta="0%" /> </set>
R.anim.slide_in_bottom
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:duration="@integer/time_duration_max" android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="0%" android:toYDelta="100%" /> </set>
And write the below line of code in your view click listener.
startActivity(new Intent(MainActivity.this, NameOfTargetActivity.class)); overridePendingTransition(R.anim.slide_out_bottom, R.anim.slide_in_bottom);
You can overwrite your default activity animation and it perform better than overridePendingTransition. I use this solution that work for every android version. Just copy paste 4 files and add a 4 lines style as below:
Create a "CustomActivityAnimation" and add this to your base Theme by "windowAnimationStyle".
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorPrimary</item> <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item> </style> <style name="CustomActivityAnimation" parent="@android:style/Animation.Activity"> <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item> <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item> <item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item> <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item> </style>
Then Create anim folder under res folder and then create this four animation files into anim folder:
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/> </set>
slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="@android:integer/config_mediumAnimTime"/> </set>
slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/> </set>
slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="@android:integer/config_mediumAnimTime"/> </set>
If you face any problem then you can download my sample project from github.
Thanks
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