8

I am trying to animate the way a new Activity appears. Default is to slide in. I have an animation set that I would like to somehow append to the Intent or the Activity so that it will start when I call startActivity.

The catch is that I need to do this completely programmatically. I cannot declare any XML resources for animations, etc. How would I do this?

user438293456
  • 596
  • 3
  • 6
  • 19
  • 1
    http://stackoverflow.com/questions/2651360/how-to-provide-animation-when-calling-another-activity-in-android – user Mar 16 '12 at 18:47

2 Answers2

1

Here is some code snippet

startActivity(intent);

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

slide_in_right

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_left

 <set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

You can tiker with the codes to get your desire effect.

Win Myo Htet
  • 5,377
  • 3
  • 38
  • 56
0

Activity has a method called overridePendingTransition() that you can use to pass in new animations. So you would just call this in the onCreate of your Activity.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • 9
    He said `completely programmatically`. – user Mar 16 '12 at 18:49
  • 2
    This is completely programmatically. I didn't say anything about modifying resource files. – CaseyB Mar 16 '12 at 18:50
  • What would I specify the animations? They need integer ids that refer to some resource either generated from my XML file (which is not what I want to do) or already provided by Google (which doesn't exist to my knowledge)... – user438293456 Mar 16 '12 at 20:19
  • 3
    @CaseyB: As mentioned, "I cannot declare any XML resources for animations". And overridePendingTransition() takes two xml resources params. – Rajkiran Mar 16 '12 at 20:42
  • There are animations built into the OS. You can use those. You needn't declare your own. – CaseyB Mar 19 '12 at 14:14