How to Control Lottie Animations Programmatically in Android?
Last Updated :
13 Aug, 2024
Lottie is a mobile library for Android and iOS that parses Adobe After Effects animations exported as JSON with Bodymovin and renders them natively on mobile. The Lottie animations are free to use vector animation files. Many famous applications use this such as Uber, Netflix, Google, Airbnb, Shopify, etc. Using Lottie, one can put animations inside a mobile (Android/iOS) application and even control them. Through this article, we would like to share with you the implementation of a method to control a JSON animation in Android using Lottie. A sample GIF 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.
Note: To use Lottie Animation in Android please refer to How to add Lottie Animation in an Android app
Steps to Control Lottie Animations
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. Note that select Kotlin as the programming language.
Step 2: Add library to the build.gradle file
For implementing an animation using Lottie, first, implement a dependency implementation 'com.airbnb.android:lottie:$lottieVersion' into the build.gradle file of the app. The latest version in October 2020 is 3.4.2, replace $lottieVersion with this value. Now sync the project by clicking the sync option which appears after every change made to the build.gradle file. Remember, build.gradle is always a Groovy or a Kotlin file.
implementation 'com.airbnb.android:lottie:3.4.2'
Step 3: Add a lottie animation file to the project
Choose any animation from here and download the JSON file of animation.

Now go to app > res > right-click > New > Folder > Raw Resource Folder and copy the JSON file to this raw folder.
Step 4: Working with the activity_main.xml file
In the activity_main.xml file declare a Lottie object by specifying the parameters. Also declare two Buttons, one to start the animation and one to pause it. The primary parameters are:
- lottie_rawRes: takes in the JSON animation to be displayed.
- lottie_loop: boolean value, that decides if the animation is on loop or not depending upon the boolean value supplied.
- lottie_autoPlay: boolean value, which decides if the animation plays as soon as it is initialized.
It is important to note that the JSON animation should only be kept in a folder named raw under the res folder since the functions are made explicitly to call this file path. And the name of the JSON file should not contain any special character other then '_'.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".MainActivity">
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animationView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:lottie_autoPlay="false"
app:lottie_loop="true"
app:lottie_rawRes="@raw/animation1" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/animationView"
android:layout_centerHorizontal="true"
android:text="Start" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnStart"
android:layout_centerHorizontal="true"
android:text="Stop" />
</RelativeLayout>
Step 5: Working with the MainActivity.kt file
Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring the buttons
val startBtn = findViewById<Button>(R.id.btnStart)
val stopBtn = findViewById<Button>(R.id.btnStop)
// On the click of startBtn
startBtn.setOnClickListener {
// is an Animation Listener
animationView.playAnimation()
}
// On the click of stopBtn
stopBtn.setOnClickListener {
// is an Animation Listener
animationView.pauseAnimation()
}
}
}
Output: Run on Emulator
Similar Reads
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 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 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 Vibrate a Device Programmatically in Android?
Hepatic feedback are also considered when it comes to user experience. So in this discussion, it's been discussed various types of haptics or the types of vibration of the device. For example, click haptics or long-press button haptics. There five different types of vibration modes in haptic feedbac
7 min read
How to Change the Screen Orientation Programmatically using a Button in Android?
Generally, the screen orientation of any application is Portrait styled. But when it comes to gaming or any other multimedia service such as watching a video, the screen orientation must change functionally from Portrait to landscape or vice-versa when the functionality is not required. So a develop
3 min read
Android Rotate animations in Kotlin
Rotate animation is a special kind of animation in Android which controls the Rotation of an object. These type of animations are usually used by developers to give a feel to the user about the changes happening in the application like loading content, processing data, etc. By using the rotate anima
4 min read
How to Increase/Decrease Screen Brightness in Steps Programmatically in Android?
Screen brightness is one such factor that directly affects the Users as well as the Battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is
5 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
Background Color Transition Animation in Android
In Android, animations can be applied to any UI element to make the application look more appealing. There are many pre-defined animations available to use in Android. However, custom animations can be created in XML format and applied to UI elements. Once such animation is color transition and in t
2 min read
How to Animate Image Rotation in Android?
In Android, ImageView is used to display images. Images can be locally stored in the program or fetched from a network and can be displayed using the ImageView. Animations can be applied to ImageView via many techniques. We can create animations in XML files and apply them to the ImageView. Follow t
2 min read