Start Activity with an Animation

Can I change the Android startActivity() transition animation?

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.

How to achieve right to left animation to start the activity

Do these modifications to your animation files:

enter.xml:

<?xml version="1.0" encoding="utf-8"?>

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

exit.xml:

<?xml version="1.0" encoding="utf-8"?>

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

You'll have your second activity sliding in from right to the left.

For a better understanding on how to play around with the fromXDelta and toXDelta values for the animations, here is a very basic illustration on the values:
Activity transition values on X axis

This way you can easily understand why you add android:fromXDelta="0%" and android:toXDelta="-100%" for your current activity. And this is because you want it to go from 0% to the -100% position.

[EDIT]

So if you want to open ActivityB from ActivityA you do the following (let's say you have a button):

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ActivityA.this, ActivityB.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
}
});

Now, if you want to have the "backwards" animation of the first one, when you leave Activity B, you'll need 2 new animation files and some code in the ActivityB's onBackPressed method, like this:

First the animation files:
left_to_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>

right_to_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>

And in ActivityB do the following:

@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}

Also if you have up navigation enabled, you'll have to add the animation in this case as well:

You enable UP navigation like this:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
}

And this is how you handle the animation in this case too:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
finish();
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
return true;
}
return super.onOptionsItemSelected(item);
}

Also be aware that even if your code is okay, your phone might have animation turned off. To turn it on, do the following:

  1. Open Settings and go to Developer Options
  2. Make sure it is enabled (By sliding the toggle button on the top right)
  3. Scroll down and under Drawing, tap these options one by one: Windows animation scale, Transition animation scale, and Animator duration scale
  4. Select "Animation scale 1x"

How to apply slide animation between two activities in Android?

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);

new Handler().postDelayed(new Runnable() {
public void run() {

/* Create an intent that will start the main activity. */
Intent mainIntent = new Intent(SplashScreen.this,
ConnectedActivity.class);
mainIntent.putExtra("id", "1");

//SplashScreen.this.startActivity(mainIntent);
startActivity(mainIntent);
/* Finish splash activity so user cant go back to it. */
SplashScreen.this.finish();

/* Apply our splash exit (fade out) and main
entry (fade in) animation transitions. */
overridePendingTransition(R.anim.mainfadein,R.anim.splashfadeout);
}
}, SPLASH_DISPLAY_TIME);
}

Animation not working properly in Activity while on start and destroy

Use this code:

overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

Slide in right xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="800" android:fromXDelta="100%" android:toXDelta="0%" />
<alpha android:duration="800" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

Slide out left xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="800" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>


Related Topics



Leave a reply



Submit