Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Create Floating Animation for RecyclerView Items in Android

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In Android, a RecyclerView is a UI element that is used to display a type of layout items in the form of a list. This list is scrollable and each element of the list is clickable. You can find more information on creating RecyclerView in this article: RecyclerView in Android with Example. RecyclerView by default has no entry or exit animation. However, animations can be created and applied to a RecyclerView. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

So in this article, we will show you how you could add an entry animation to a RecyclerView in Android. Follow the below steps once the IDE is ready, but first, it is important to understand the application that we created and explained in this article.

Understanding the application structure

The application recalls a basic RecyclerView in the activity_main.xml. The elements displayed in the RecyclerView are an array of strings generated in the main code. RecyclerView item layout is recycler_view_item_layout.xml and the RecyclerView adapter is MyAdapter.kt. The main code is the MainActivity.kt where the adapter is implemented to send the generated strings to the RecyclerView.

Animation i.e. right_to_left.xml and animation layout i.e.  layout_right_to_left.xml are created in the anim folder. To create anim folder, right-click on the res folder, select new, click on Android Resource Directory. A New Resource Directory window appears. Set Directory name as anim, Resource type as transition, and click OK. Once the anim folder is created, right-click on it, select new, and click on Animation Resource File. Set the File name as right_to_left and Root element to set and click OK. Repeat the same process for layout_right_to_left, name it and set Root element to layoutAnimation and click OK. Now both the animation and animation layout files are ready. Animation is programmed inside right_to_left.xml and animation layout in layout_right_to_left.xml. The animation would be statically called in the animation layout file and the animation layout file would be dynamically called in the main code to apply this layout to the RecyclerView.

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. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working on anim > right_to_left.xml

Sample right-to-left animation code is shown below. This animation would start and complete in 0.5 secs/ 500 milli-secs.


<?xml version="1.0" encoding="utf-8"?>
        android:toXDelta="0" />
        android:toAlpha="1" />



Step 3: anim > layout_right_to_left.xml


A simple layout code is shown below. The above animation is added in the layout attributes. Delay is applied to next items so all the items don’t appear at once.



<?xml version="1.0" encoding="utf-8"?>
    android:delay="15%" />



Step 4: 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. Add a RecyclerView and a Floating Button as shown below. The RecyclerView would be used to display the strings and Button would be used to display the RecyclerView on-click.



<?xml version="1.0" encoding="utf-8"?>



Step 5: recycler_view_item_layout.xml


The item layout code is shown below. It consists of a TextView to display a string.



<?xml version="1.0" encoding="utf-8"?>



Step 6: Working with the MyAdapter.kt file


The below code is for the RecyclerView adapter. Explanation of similar code is available at the article mentioned in the introduction of this article.



package org.geeksforgeeks.recyclerviewfloatanimation
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class MyAdapter(mData: ArrayList<String>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
    private val mDataCopy = mData
    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view), View.OnClickListener {
        val tvCard: TextView = view.findViewById(
        override fun onClick(v: View?) {
            TODO("Not yet implemented")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            .inflate(R.layout.recycler_view_item_layout, parent, false))
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.tvCard.text = mDataCopy[position]
    override fun getItemCount(): Int {
        return mDataCopy.size



Step 7: 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.



package org.geeksforgeeks.recyclerviewfloatanimation
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.animation.AnimationUtils
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing the
        // RecyclerView from the layout file
        val mRecyclerView = findViewById<RecyclerView>(
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        // Generating data (array of strings)
        val mDataArray = mGenerateData()
        // Initializing the adapter
        val mAdapter = MyAdapter(mDataArray)
        // Declaring and initializing the
        // Floating Button from the layout file
        val mFloatingButton = findViewById<FloatingActionButton>(
        // When Button is clicked, animation is applied
        // to the RecyclerView and RecyclerView is displayed
            val controller = AnimationUtils.loadLayoutAnimation(this, R.anim.layout_right_to_left)
            mRecyclerView.layoutAnimation = controller
            mRecyclerView.adapter = mAdapter
    // Sample function to return array
    // of strings (Item 1, Item 2, Item 3,.....)
    private fun mGenerateData(): ArrayList<String> {
        val mArrayList = arrayListOf<String>()
        for (i in 0..10) {
            mArrayList.add("Item $i")
        return mArrayList





You can see that the animation is applied to the RecyclerView items on the Floating Button click.



My Personal Notes arrow_drop_up
Last Updated : 16 Feb, 2022
Like Article
Save Article
Similar Reads