How to insert Slide From Bottom animation in RecyclerView in Android - GeeksforGeeks

08 Jul, 2020
In this article, the animation that makes the items slide from the bottom is added in the recycler view. Here we don`t use any other library to add the animation. Adding animations make the application attractive and give a better user experience.
Approach:Step 1: Create  “anim”  resource directory. Right-click on res folder and follow path 
res -> new -> Android Resource Directory
 From the Resource type, choose “anim” and don’t change  Directory name then press ok.Step 2: Create an Animation file.Right-click on “anim” directory and create a new Animation Resource File. 
anim -> new -> Animation Resource File -> create “slide_from_bottom” xml file.
Add the below code in slide_from_bottom.xml file. Here the animation is defined. 
XML
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="600"><translate    android:fromYDelta="50%p"    android:interpolator="@android:anim/accelerate_decelerate_interpolator"    android:toYDelta="0"/><alpha    android:fromAlpha="0"    android:interpolator="@android:anim/accelerate_decelerate_interpolator"    android:toAlpha="1"/></set>
Step 3: Create one more animation file to hold “slide_from_bottom.xml” 
anim -> new -> Animation Resource File -> create “layout_animation_slide_from_bottom” xml file
 Add the below code in the XML file that is just created. Here, animation slide_from_buttom  is added that is defined in the previous step.
XML
<?xml version="1.0" encoding="utf-8"?><layoutAnimation  xmlns:android="http://schemas.android.com/apk/res/android"  android:animation="@anim/slide_from_bottom"  android:animationOrder="normal"  android:delay="15%"></layoutAnimation>
Step 4:(Final) Call that animation in Your RecyclerView. In the tag layoutAnimation, add layout_animation_slide_from_bottom.xml. Now while displaying the list items in recycler view, the items will add with the animation that is carried by the layout_animation_slide_from_bottom.xml and defined in slide_from_bottom.xml.
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="#6F6A6A"    tools:context=".MainActivity">      <androidx.recyclerview.widget.RecyclerView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layoutAnimation="@anim/layout_animation_slide_from_bottom"        android:orientation="vertical"        android:id="@+id/recyclerView"        />  </androidx.constraintlayout.widget.ConstraintLayout>
Output:
android
How To
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Align Text in HTML?
How to Install FFmpeg on Windows?
How to Install Jupyter Notebook on MacOS?
How to Install Python Pandas on MacOS?
How to Override compareTo Method in Java?
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Initialize an ArrayList in Java
Arrays.sort() in Java with examples