Mastering Android Animations in Kotlin
In the world of Android development, animations breathe life into your application, making interactions more dynamic and engaging. Animations involve rendering a series of frames or transitions to create the illusion of motion. Fortunately, Android offers a rich set of APIs and XML attributes to create stunning animations with minimal effort. In this guide, we’ll walk through the process of implementing various animations using Kotlin.
ANDROID


Mastering Android Animations in Kotlin
In the world of Android development, animations breathe life into your application, making interactions more dynamic and engaging. Animations involve rendering a series of frames or transitions to create the illusion of motion. Fortunately, Android offers a rich set of APIs and XML attributes to create stunning animations with minimal effort. In this guide, we’ll walk through the process of implementing various animations using Kotlin.
Understanding XML Attributes for Animations
To get started with animations, it's essential to understand some commonly used XML attributes. Below is a breakdown of key attributes used to configure animations in Android:
android:duration - Specifies how long the animation should run (in milliseconds).
android:fromAlpha - Defines the starting transparency value (1.0 for fully visible, 0.0 for fully invisible).
android:toAlpha - Sets the ending transparency value.
android:id - Assigns a unique identifier to the view.
android:fromYDelta - Specifies the starting Y-coordinate position.
android:toYDelta - Sets the ending Y-coordinate position.
android:startOffset - Introduces a delay before the animation starts.
android:pivotX - Determines the X-axis pivot point for scaling or rotation.
android:pivotY - Determines the Y-axis pivot point for scaling or rotation.
android:fromXScale - Initial X-axis scale value.
android:fromYScale - Initial Y-axis scale value.
android:toXScale - Final X-axis scale value.
android:toYScale - Final Y-axis scale value.
android:fromDegrees - Starting rotation angle in degrees.
android:toDegrees - Final rotation angle in degrees.
android:interpolator - Controls the pace of change during the animation.
Setting Up a New Android Project
Before we dive into animations, let’s quickly create a new Android project:
Open Android Studio.
Select File > New > New Project.
Choose Empty Activity and click Next.
Name your application as DynamicEditTextKotlin.
Select Kotlin as the programming language and set the minimum SDK to 21 or higher.
Click Finish to generate your project.
If you've already created the project, you can skip this step and proceed to implement animations.
Designing the User Interface
To create our animation demo, we will modify the activity_main.xml file to include a TextView for displaying animations and a set of buttons to trigger different animation effects.
Updated activity_main.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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearLayout"
android:gravity="center"
android:text="Geeks for Geeks"
android:textSize="32sp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<!-- Buttons for Various Animations -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/fade_in"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Fade In" />
<Button
android:id="@+id/fade_out"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Fade Out" />
</LinearLayout>
<!-- Additional Buttons for Animations -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/zoom_in"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Zoom In" />
<Button
android:id="@+id/zoom_out"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Zoom Out" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Creating Animation Resources
To animate our views, we’ll create an anim folder in the res directory and add various animation XML files. These files define the behavior and attributes of animations.
Steps to Create Animation Files:
Right-click on res → Select New → Android Resource Directory.
Set the directory name to anim.
Add the following XML files to define different animation effects:
1. bounce.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="100%"
android:toYDelta="-20%"
android:duration="300" />
<translate
android:startOffset="500"
android:fromYDelta="-20%"
android:toYDelta="10%"
android:duration="150" />
<translate
android:startOffset="1000"
android:fromYDelta="10%"
android:toYDelta="0"
android:duration="100" />
</set>
2. fade_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
3. fade_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
4. rotate.xml
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" />
Implementing Animations in MainActivity
Once our XML animations are in place, the next step is to load and apply them using Kotlin code. We’ll do this in MainActivity.kt by setting up listeners for button clicks and triggering corresponding animations.
MainActivity.kt Code:
package net.geeksforgeeks.animationsinkotlin
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.view.animation.AnimationUtils
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)
// Fade In Animation
fade_in.setOnClickListener {
textView.visibility = View.VISIBLE
val animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in)
textView.startAnimation(animationFadeIn)
}
// Fade Out Animation
fade_out.setOnClickListener {
val animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out)
textView.startAnimation(animationFadeOut)
Handler().postDelayed({
textView.visibility = View.GONE
}, 1000)
}
// Zoom In Animation
zoom_in.setOnClickListener {
val animationZoomIn = AnimationUtils.loadAnimation(this, R.anim.zoom_in)
textView.startAnimation(animationZoomIn)
}
// Zoom Out Animation
zoom_out.setOnClickListener {
val animationZoomOut = AnimationUtils.loadAnimation(this, R.anim.zoom_out)
textView.startAnimation(animationZoomOut)
}
}
}
Configuring the Manifest File
To complete our setup, ensure the AndroidManifest.xml file contains the following configuration:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.geeksforgeeks.animationsinkotlin">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Conclusion
By following this tutorial, you have successfully implemented various animations such as fade, zoom, slide, bounce, and rotation in your Android application using Kotlin. These animations enhance the user experience, making your app more interactive and appealing. Explore these techniques further to create immersive and visually pleasing applications.