2

I am new to Android. When I open a new activity, I need to create an fade in animation for the opening activity. eg: Currently I am in activity "A", now I want to open another activity "B" on a button click. Now I could open an activity without any animation. But I would like to give a Fade In animation. Now, following code is used to open an activity.

Intent profileActivity = new Intent(Login.this, Profile.class);
startActivityForResult(profileActivity, 0);

Here "profileActivity" should open with a Fade In animation. What could I make in the above code to make an animation.

Thanks in advance.

sree_iphonedev
  • 3,514
  • 6
  • 31
  • 50
  • Similar questions: http://stackoverflow.com/questions/2651360/how-to-provide-animation-when-calling-another-activity-in-android, http://stackoverflow.com/questions/3389501/activity-transition-in-android – kgiannakakis May 17 '12 at 09:19

5 Answers5

7

Just add this line to your calling intent :

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

it will look like that :

Intent intent = new Intent(ActivityA.this, ActivityB.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);

Then create an "anim" folder under your "res" folder and add these 2 xml files

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="100%"
    android:toXDelta="0%" />

slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="0"
    android:toXDelta="-100%" />

As you can see I'm just doing a translation animation if you want to add a fading effect just add

android:fromAlpha="0.0" android:toAlpha="1.0"

Hope it will help.

moujib
  • 742
  • 2
  • 7
  • 26
7

This is the code of fade animation R.anim.fade

 <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="@android:integer/config_longAnimTime" />
DynamicMind
  • 4,240
  • 1
  • 26
  • 43
2

thank for @moujib answer, I've never known that before. What I did is to manually apply the animation as soon as the second activity is started. Using overridePendingTransition() is a lot cleaner (it's available in api 5 or up). Aside to @moujib answer, you don't have to define the fade in animation by yourself as Android already had it defined. Take a look here http://developer.android.com/reference/android/R.anim.html

Warut Surapat
  • 503
  • 6
  • 12
  • Ah. That developer link was helpful. Thanks. I was almost considering checking that API SDK samples' project. :) – Sufian Nov 14 '13 at 13:21
0

Android Animation

U can put your welcome page with progress bar the above link will help you how to add animation in a activity

Thiru VT
  • 831
  • 2
  • 8
  • 24
0

Just use this line of code for smooth transition from one activity to other:

startActivity(Intent(this, DestinationActivity::class.java),ActivityOptions.makeCustomAnimation(this,R.anim.abc_fade_in, R.anim.abc_fade_out).toBundle())