Can I change the Android startActivity() transition animation?
Solution 1
In the same statement in which you execute finish(), execute your animation there too. Then, in the new activity, run another animation. See this code:
fadein.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500"/> //Time in milliseconds
</set>
In your finish-class
private void finishTask() {
if("blabbla".equals("blablabla"){
finish();
runFadeInAnimation();
}
}
private void runFadeInAnimation() {
Animation a = AnimationUtils.loadAnimation(this, R.anim.fadein);
a.reset();
LinearLayout ll = (LinearLayout) findViewById(R.id.yourviewhere);
ll.clearAnimation();
ll.startAnimation(a);
}
fadeout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500"/>
</set>
In your new Activity-class you create a similiar method like the runFadeAnimation I wrote and then you run it in onCreate and don't forget to change the resources id to fadeout.
Solution 2
Starting from API level 5 you can call overridePendingTransition immediately to specify an explicit transition animation:
startActivity();
overridePendingTransition(R.anim.hold, R.anim.fade_in);
or
finish();
overridePendingTransition(R.anim.hold, R.anim.fade_out);
Solution 3
See themes on android: http://developer.android.com/guide/topics/ui/themes.html.
Under themes.xml there should be android:windowAnimationStyle
where you can see the declaration of the style in styles.xml.
Example implementation:
<style name="AppTheme" parent="...">
...
<item name="android:windowAnimationStyle">@style/WindowAnimationStyle</item>
</style>
<style name="WindowAnimationStyle">
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
Solution 4
Use overridePendingTransition
startActivity();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set>
fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/anticipate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>
Solution 5
For fadeIn and fadeOut, only add this after super.onCreate(savedInstanceState) in your new Activity class. You don't need to create something else (No XML, no anim folder, no extra function).
overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
coneybeare
http://matt.coneybeare.me @coneybeare I have had a ton iOS apps under my name and my business name. I have multiple client apps, and one from an established startup. Many of my apps have been featured by Apple. I developed two iOS apps that made it into the top 100. One of the apps held the number two spot for three weeks. Another floats in/out of the paid News top 10. My apps have over 10 million downloads. I do a ton of web work too using rails for all my sites. I do much of my own design for all the sites I have created.
Updated on December 04, 2021Comments
-
coneybeare over 2 years
I am starting an activity and would rather have a alpha fade-in for
startActivity()
, and a fade-out for thefinish()
. How can I go about this in the Android SDK? -
RightHandedMonkey over 10 yearsAdd something like: @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.hold, R.anim.fade_out); } for adding back animations.
-
elimirks over 10 yearsIn addition, it may be better to use the default short animation time:
android:duration="@android:integer/config_shortAnimTime"
-
Gelldur almost 10 years
-
cy198706 almost 9 yearsThis should be the best clean answer.
-
vovahost over 8 yearsUpdate links for themes.xml and styles.xml android.googlesource.com/platform/frameworks/base/+/refs/heads/… android.googlesource.com/platform/frameworks/base/+/refs/heads/…
-
Choletski about 8 yearswhere is
runFadeAnimation()
-
Itiel Maimon over 6 years@RightHandedMonkey For adding back animations better override the finish(); method of the activity for case in which the activity ends by something else than the back button (e.g. a custom exit button...).
-
Android developer about 6 yearsGuys, please be sure to see the real answer below this post.
-
Farid Z almost 6 yearsoverridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
-
Abhishek kumar over 5 yearsBest Solution .
-
Ali Khaki over 5 yearswhat is blabbla ??!!
-
Tamoxin about 5 yearsHow would you access
@android:anim/fade_in
from the java code?