How to Add CrossFade Animation Between Activities in Android?
Last Updated :
23 Feb, 2021
Android animations are the object which can be said the most important factor for a better user-interface and user-experience in an android application. In a wide series of articles on GeeksforGeeks for making a better UI, UX of an application, this is also going to be the one. In this article, we will know how to add crossfade animation between two activities in android. The meaning of CrossFading here is a smooth transition between two activities. Usually, It means the fading out the one, while fading in another activity. It creates a smooth transition, and for a short period of time users will get a feel for both the activities. Here's a sample of the same type of animation we are going to create between activities. Note that we are going to implement this project using both Java and Kotlin language.

Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. You can choose the language as per your choice because here we are giving the code for kotlin as well as java.
Step 2: Before going to the coding section first you have to do some pre-task
- Modify the strings.xml file:
XML
<resources>
<string name="app_name">GFG App</string>
<string name="go_to_main_activity">GO TO MAIN ACTIVITY</string>
<string name="go_to_second_activity">GO TO SECOND ACTIVITY</string>
</resources>
- Modify the colors.xml file:
XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#00AC28</color>
<color name="colorPrimaryDark">#09d639</color>
<color name="colorAccent">#03DAC5</color>
</resources>
Step 3: Create another empty activity
Make sure that you have selected Android for project structure on the top left corner of the screen. Then navigate to java/your_package_name. Now, right-click on your package name and select new here, and select activity and empty activity.
Just give it a name of your choice like we are giving it SecondActivity and click on finish.
Step 4: Working with the layout files
Go to, res/layout/activity_second, and paste the below code. Here, we are using a ConstraintLayout with a Button for switching to another activity.
Below is the code for the activity_second.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09d639"
tools:context=".SecondActivity">
<Button
android:id="@+id/btnSecond"
android:layout_width="130dp"
android:layout_height="70dp"
android:background="#fff"
android:onClick="goToMainActivity"
android:text="@string/go_to_main_activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="AN GFG APP WAS MADE DURING TUTORIAL"
android:textColor="@android:color/white"
android:textSize="19sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSecond" />
</androidx.constraintlayout.widget.ConstraintLayout>
Also, navigate to the res/layout/activity_main and remove all the default code and paste the below code. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<Button
android:id="@+id/btnFirst"
android:layout_width="130dp"
android:layout_height="70dp"
android:onClick="goToSecondActivity"
android:text="@string/go_to_second_activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="OnClick" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Working with the anim files
Please refer to How to Create Anim Folder & Animation File in Android Studio to create the anim folder and the animation file. We have created two animation files and name the file fade_in and fade_out. Now, remove all the default code from fade_in and paste the below code. Below is the code for the fade_in.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="400"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
Below is the code for the fade_out.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="700"
android:fillAfter="true"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
Step 6: Working with the MainActivity file
Just add the following code in your MainActivity class after the onCreate() method. This is the code for going to SecondActivity and we are setting the animation between it.
Java
public void goToSecondActivity (View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
Kotlin
fun goToSecondActivity(view: View?) {
val intent = Intent(this@MainActivity, SecondActivity::class.java)
startActivity(intent)
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}
Step 7: Working with the SecondActivity file
Also, add the following code in your SecondActivity after the onCreate() method.
Java
public void goToMainActivity (View view) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
Kotlin
fun goToMainActivity(view: View?) {
val intent = Intent(this@SecondActivity, MainActivity::class.java)
startActivity(intent)
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}
Output:
GitHub link: https://github.com/shivamparashar165/crossfade_android_anim
Similar Reads
How to add Slide animation between activities in android?
In this article, we will learn about how to add slide screen animation between different activities to make UX better. Apps are made up of many activities and to navigate between the activities slide screen animation can be very useful. Animation plays a very critical role in any app if the app has
4 min read
How to Add Page Curl Animation Between Images in Android?
Animations are the best way to create a good looking user-interfaces. Users love to see animation in an android application. And this becomes even more necessary when we have to make some kind of special apps, For example, if we are creating an application for a book Then we should create an animati
3 min read
How to make Check/Tick and Cross animations in Android
AnimatedVectorDrawable class was introduced in API 21 and is used to animate Vector Drawables beautifully and easily. Using AnimatedVectorDrawable, one can: Rotate, Scale, Translate VectorDrawables Animate the VectorDrawable such as fill color etc. Draw paths and do Path Morphing An AnimatedVectorDr
4 min read
How to Add Uber Car Animation in Android App?
Google Map is being used in a lot of applications these days. Many apps need google maps services for multiple purposes. Example of such apps is our daily life applications like Zomato, Swiggy and amazon uses google maps for delivery purposes while apps like uber and ola use maps for tracking the re
7 min read
How to add fading TextView animation in Android
TextView is the basic building block of user interface components. It is used to set the text and display it to the user. It is a very basic component and used a lot. A Fading TextView is a TextView that changes its content automatically every few seconds. If we want to design a beautiful interface
1 min read
How to Create Star Animation in Android?
In this article, we are going to create star animation using an animated star library. Here we will be creating a background drawable file and will specify the color for the animation. The star animation we have created is easy to create since we are using a library. A sample GIF is given below to g
3 min read
How to Finish All the Previous Activities in an Android Application?
Android applications are having so many activities present inside them for different functionalities. When a user performs some action within the application he will be navigated from one screen to another. When the user is navigated to the next screen the previous activity remains open in the stack
4 min read
How to Change Application's Starting Activity in Android?
Many times while building an android application. We have to change the default activity of our application to another activity that we have created within our android studio project. So that we can set that custom activity to the starting activity of our application. In this article, we will take a
2 min read
How to add Lottie Animation in an Android app
This article is about enhancing the User Interface of the mobile application by adding Lottie animation files into our mobile app. The Lottie animations are free to use vector animation files. These animation files can be found at the official site here. Many famous applications use this such as Ube
2 min read
How to create AnimatedGradient in Android?
In this article, we are going to learn how to create AnimatedGradient in android. It can be used in the background of our app. In this, we add different color gradients and animate them. Even in the older versions of Instagram, the developers used AnimatedGradient for the background of the login scr
2 min read