Activity Transition in Android
Last Updated :
24 Apr, 2025
The term "Android activity transitions" refers to a complex and multifaceted set of processes and mechanisms that are developed when moving between two distinct activities within an Android application. The goal of these transitions is to enhance an experience that is both seamlessly integrated such that the user is able to effortlessly glide between various constituent parts of the application.
Another prominent example of an activity transition is that of the slide transition, which involves a progressive and gradual sliding of one activity off of the screen, while a new activity simultaneously glides onto the screen in its place. Such a transition can be used to enhance a sense of continuity between different constituent parts of an application, such that the user feels as though they are still situated within the same application even as they are shifting and moving between various different activities.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2:
Create Two Activities to which you want to add animation. Here is my XML code for MainActivity
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="@color/purple_200"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonOpenSecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Open Second Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code For Second Activity:
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="@color/teal_200"
tools:context=".SecondActivity">
<Button
android:id="@+id/buttonGoBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3:
Create One Android Resource Directory of type "anim" in my case we are using directory name "anim" only
Step 4:
On that anim folder add the animations that you want to add in switching between two activities you have many options. Here is the list that we used.
slide_from_bottom.xml
XML
<!--slide from bottom-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
<translate android:fromYDelta="100%"
android:toYDelta="0%"/>
</set>
slide_to_top.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
<translate
android:fromYDelta="0%"
android:toYDelta="-100%"/>
</set>
slide_from_left.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"/>
</set>
slide_from_right.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
<translate
android:fromXDelta="100%"
android:toXDelta="0%"/>
</set>
zoom_in.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
<scale
android:fromXScale="0.8"
android:fromYScale="0.8"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0%"
android:toYScale="1.0%" />
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
</set>
zoom_out.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.8%"
android:toYScale="0.8%" />
<alpha
android:fromAlpha="1"
android:toAlpha="0"/>
</set>
static_animation.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
</set>
These are some examples that we used for animation.
Step 5:
Use this animation in your activity to configure when the user clicks then show this animation
MainActivity.Java
Java
package com.android.gfgapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.buttonOpenSecondActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,SecondActivity.class));
// for right to left animation
// overridePendingTransition(R.anim.slide_from_right,R.anim.slide_to_left);
// for bottom to top animation
// overridePendingTransition(R.anim.slide_from_bottom,R.anim.slide_to_top);
// for top to bottom animation
// overridePendingTransition(R.anim.slide_from_top,R.anim.slide_to_bottom);
// zoom-in animation
// overridePendingTransition(R.anim.zoom_in,R.anim.static_animation);
// zoom-out animation
// overridePendingTransition(R.anim.static_animation,R.anim.zoom_out);
}
});
}
}
Output:
Similar Reads
Introduction to Activities in Android
Activity class is one of the very important parts of the Android Component. Any app, don't matter how small it is (in terms of code and scalability), has at least one Activity class. Unlike most programming languages, in which the main() method is the entry point for that program or application to s
6 min read
How to Restart Activity in Android?
An Activity in Android is that element of the application that provides a platform for drawing UI elements. Activity, in general, occupies the entire screen but may not always fill the entire screen. MainActivity is the first activity that is created when a project is created. However, activities ca
2 min read
What is Android Activity "launchMode"?
Launch mode is an Android OS command that determines how the activity should be started. It specifies how every new action should be linked to the existing task. Before proceeding, you must first grasp the following critical topics: TasksBack StackTypes of Launch Modes for Activities Without further
4 min read
How to Create a Transparent Activity in Android?
Many times we have to display a feature in our app where we have to use transparent activity inside our android app. In this article, we will take a look at creating a Transparent Activity in Android. What we are going to build in this article? We will be building a simple application in which we
3 min read
Activity State Changes In Android with Example
Prerequisites: Activity lifecycle in android As it is known that every Android app has at least one activity associated with it. When the application begins to execute and runs, there are various state changes that activity goes through. Different events some user-triggered and some system triggered
6 min read
What is "Donât Keep Activities" in Android?
Have you ever came across an option under Developer Settings on your mobile phone and wondered what is it all about? If you play around with your phone's settings, then you will discover an option depicting "Don't keep activities" hidden under "Developer Options". For this, you have to be in develop
3 min read
Activity Aliases in Android to Preserve Launchers
Before moving on to the topic, pick up your mobile phones and count the number of applications that you are having on your device. All of you must have more than 30 applications on average. But out of these 30 apps, we only use 5â6 applications on a regular basis. Other applications are rarely used
7 min read
How to Go Back to Previous Activity in Android?
In this article, we are going to see how we can add a back button to an activity through which we can go back to its previous activity. This can be achieved with just a few lines of code, which is explained in the steps below Step by Step ImplementationStep 1: Create a New Project in Android StudioT
3 min read
Interesting Facts About Android
Android is a Mobile Operating System that was released on 23, September 2008. Android is free, open-source operating system and is based on modified version of Linux kernel. Open Handset Alliance (OHA) developed the Android and Google commercially sponsored it. It is mainly designed for touchscreen
3 min read
Data Binding in Android: Activities, Views, and Fragments
Data binding in Android is one of the most effective features for Android developers to bind the UI controls present in the layout with the data resources in the code. This makes the code less complicated and readable thus enhancing the overall maintainability of the application. In this article, I
4 min read