Android Shared Element Transition with Kotlin
Last Updated :
21 Dec, 2022
Shared element transition is seen in most of the applications which are used when the user navigates between two screens. This type of animation is seen when a user clicks on the items present in the list and navigates to a different screen. During these transitions, the animation which is displayed is called a shared element transition. In this article, we will take a look at the implementation of Shared element transition in Android applications using Kotlin.
Note: If you are looking to integrate shared element transition in android using Java. Check out the following article: Shared Element Transition in Android using Java
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 Kotlin as the programming language.
Step 2: Create a New Empty Activity
Navigate to the app > java > your package name > right-click > New > Activity > Empty Activity and name the activity as MainActivity2.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- Textview to display the heading of the app-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:padding="5dp"
android:text="Shared Element Transition Example"
android:textAlignment="center"
android:textColor="@color/purple_200"
android:textSize="20sp"
android:textStyle="bold" />
<!--on below line we are creating a simple image view-->
<ImageView
android:id="@+id/idIVAndroid"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:padding="10dp"
android:src="@drawable/android"
android:transitionName="fade" />
</RelativeLayout>
Step 4: Working with the activity_main2.xml file
Navigate to app > res > layout > activity_main2.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<!--on below line we are creating a simple image view
and specifying a transition name for it -->
<ImageView
android:id="@+id/idIVAndroid"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:padding="10dp"
android:src="@drawable/android"
android:transitionName="fade" />
</RelativeLayout>
Step 5: Working with the MainActivity2.kt file
Go to the MainActivity2.kt file and refer to the following code. Below is the code for the MainActivity2.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.os.Build
import android.os.Bundle
import android.transition.Fade
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
class MainActivity2 : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
// on below line we are creating
// a variable for fade animation
val fade = Fade()
// on below line we are excluding our target such
// as status bar background and navigation bar
// background from animation.
fade.excludeTarget(android.R.id.statusBarBackground, true)
fade.excludeTarget(android.R.id.navigationBarBackground, true)
// on below line we are setting enter
// and exit transition as fade.
window.enterTransition = fade
window.exitTransition = fade
}
}
Step 6: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.transition.Fade
import android.widget.ImageView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.core.view.ViewCompat
class MainActivity : AppCompatActivity() {
// on below line we are creating
// a variable for our image view.
lateinit var imageView: ImageView
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line we are creating
// a variable for our fade.
val fade = Fade()
// on below line we are excluding the target
// which we dont want to animate such as our
// status background and navigation background.
fade.excludeTarget(android.R.id.statusBarBackground, true)
fade.excludeTarget(android.R.id.navigationBarBackground, true)
// on below line we are specifying
// enter transition as fade.
window.enterTransition = fade
// on below line we are specifying
// exit transition as fade.
window.exitTransition = fade
// on below line we are initializing
// our image view with its id.
imageView = findViewById(R.id.idIVAndroid)
// on below line we are adding on
// click listener for our image view.
imageView.setOnClickListener {
// on below line we are creating a variable
// for our intent to open a new activity.
val intent = Intent(this@MainActivity, MainActivity2::class.java)
// on below line we are creating a variable
// for activity options compact and setting
// transition for our activity.
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(
this@MainActivity, imageView, ViewCompat.getTransitionName(imageView)!!
)
// on below line we are starting our
// activity passing bundles.
startActivity(intent, options.toBundle())
}
}
}
Now run your application to see the output of it.
Output:
Similar Reads
Shared Element Transition in Android with Example
Shared Element Transition is one of the most seen animations in Android apps. This type of animation is used when we have to open an item from a ListView or RecyclerView. Shared Element Transition in Android determines how shared element views are animated from activity to activity or fragment to fr
4 min read
Android - Implement Preference Setting Screen with Kotlin
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at im
5 min read
Android - Deep Linking with Kotlin
Deep Links are seen in most applications where users can use these links to redirect to any specific screen or activity within the application. We can also pass data using these links within our application. In this article, we will take a look at Implementing Deep Links in Android applications usin
5 min read
Android - Crash in Activity Transitions with SharedElement
Shared element transition is a powerful animation feature in Android that allows developers to create a seamless transition between different elements in their app, giving the illusion of a smooth flow between screens. However, sometimes the transition may not work as expected, and the animation may
4 min read
Implement Android Pull-to-Refresh with ListVIew using Kotlin
Swipe to Refresh Layout is used in most social media applications such as Facebook or Instagram. In these applications, users can simply swipe down to refresh the posts or feeds within the applications to load the new ones. Swipe to refresh layout detects vertical swipe and displays a progress bar.
7 min read
Android App Development with Kotlin: A Technical Overview
Android Kotlin app development is a popular way to create mobile applications for the Android platform. Kotlin is a modern programming language that is designed to be easy to use and understand, making it a great choice for developers who want to create apps quickly and efficiently. In this article,
7 min read
Android View Shaker in Kotlin
View Shaker is an android animation in which the UI of the screen vibrates for a specific interval of time. We can implement this View shaker to the specific views of the application. View Shaker provides different animation effects which we can add to our views such as bounce, fade, float, and othe
4 min read
ObjectAnimator in Android with Example
ObjectAnimator is a Subclass of ValueAnimator, which allows us to set a target object and object property to animate. It is an easy way to change the properties of a view with a specified duration. We can provide the end position and duration of the animation. Let us create a simple version of the G
5 min read
Android - Swipe to Delete and Undo in RecyclerView with Kotlin
We have seen many android applications in which data is being displayed in the form of a list. If we want to delete any item from that recycler view we can simply swipe that item to delete it. We can see this type of feature in the Gmail application in which when an email is swiped to the right it i
7 min read
ViewAnimator in Android with Example
ViewAnimator is a very fascinating and useful feature as it switches between two or more views smoothly and mainly meant for animation features of the views on screens. It is the parent class of ViewFlipper and ViewSwitcher and the main distinction is it can switch between more than 2 views also. It
4 min read