Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to Implement Glassmorphism in Android?
Next article icon

How to Implement Fab Explosion Animation in Android?

Last Updated : 04 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

When interacting with apps on our devices, animations add delight to the user's experience and create a sense of connection between the vibrant destinations. One of the animations on Android which enhances the user's experience is the FAB (Floating Action Button) Explosion Animation. Here, the user clicks the FAB and it explodes to fill the whole screen, with the following screen being loaded. Before the explosion, the FAB shrinks a small amount to account for the press from the user- this coupled with the explosion, creates a pleasant journey to the next screen. A sample video is given below to get an idea about what we are going to do in this article.



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: Navigate to the Gradle Scripts > build.gradle(Module:app) and enables ViewBinding by adding the given code above BuildTypes and Sync the project by clicking on Sync Now option appearing in the top right corner.

buildFeatures {
viewBinding true
}

Now update SDK Version and Sync the project by clicking on Sync Now option appearing in the top right corner.

android {
compileSdkVersion 31
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.plcoding.fabexplosionanimation"
minSdkVersion 21
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Step 3: Add the FAB Icons to the Drawable File. Right-click on the drawable folder > New > Vector Asset. Add choose a vector asset of your choice.

Step 4: Navigate to the res< drawable, right-click on drawable, and choose New > Drawable Resource File. Change the file name to a circle and add the below code.

XML
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="oval">     <solid android:color="@color/purple_500" /> </shape> 

Step 5: 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"?> <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"     tools:context=".MainActivity">      <com.google.android.material.floatingactionbutton.FloatingActionButton         android:id="@+id/fab"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:backgroundTint="@color/purple_500"         android:src="@drawable/ic_baseline_baby_changing_station_24"         android:layout_marginEnd="16dp"         android:layout_marginBottom="16dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:tint="@color/white" />      <View         android:id="@+id/circle"         android:layout_width="64dp"         android:layout_height="64dp"         android:background="@drawable/circle"         android:visibility="invisible"         app:layout_constraintBottom_toBottomOf="@+id/fab"         app:layout_constraintEnd_toEndOf="@+id/fab"         app:layout_constraintStart_toStartOf="@+id/fab"         app:layout_constraintTop_toTopOf="@+id/fab" />  </androidx.constraintlayout.widget.ConstraintLayout> 

Step 6: Right-click on the res > New > Drawable Resource File. Choose the Resource Type as Animation and the file name as circle_explosion_anim.

Screenshot-2023-06-19-171132.png
Resource file for explosion animation

Add the below code in the Resource file.

XML
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">     <scale android:pivotX="50%"         android:pivotY="50%"         android:fromXScale="0"         android:fromYScale="0"         android:toXScale="40"         android:toYScale="40" /> </set> 

Step 7: Create a new Kotlin Class and name it ViewExt and refer to the below code to it.

Kotlin
package com.plcoding.fabexplosionanimation  import android.view.View import android.view.animation.Animation  fun View.startAnimation(animation: Animation, onEnd: () -> Unit) {     animation.setAnimationListener(object : Animation.AnimationListener {         override fun onAnimationStart(animation: Animation?) = Unit          override fun onAnimationEnd(animation: Animation?) {             onEnd()         }          override fun onAnimationRepeat(animation: Animation?) = Unit     })     this.startAnimation(animation) } 

Step 8: Working with the MainActivity.kt file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Kotlin
package com.plcoding.fabexplosionanimation  import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.animation.AccelerateDecelerateInterpolator import android.view.animation.AnimationUtils import androidx.core.content.ContextCompat import androidx.core.view.isVisible import com.plcoding.fabexplosionanimation.databinding.ActivityMainBinding  class MainActivity : AppCompatActivity() {      private lateinit var binding: ActivityMainBinding      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         binding = ActivityMainBinding.inflate(layoutInflater)         setContentView(binding.root)          val animation = AnimationUtils.loadAnimation(this, R.anim.circle_explosion_anim).apply {             duration = 700             interpolator = AccelerateDecelerateInterpolator()         }          binding.fab.setOnClickListener {             binding.fab.isVisible = false             binding.circle.isVisible = true             binding.circle.startAnimation(animation) {                 // display your fragment                 binding.root.setBackgroundColor(ContextCompat.getColor(this, R.color.purple_500))                 binding.circle.isVisible = false             }         }     } } 

Output:




Next Article
How to Implement Glassmorphism in Android?
author
utkarshrajmishra8975
Improve
Article Tags :
  • Kotlin
  • Android

Similar Reads

  • How to Implement Glassmorphism in Android?
    Glassmorphism or BlurView is a style, developed by adopting the behavior of glass to enhance your app design. It gives a look and feel of translucent or transparent elements. Glassmorphic elements and shapes work really well on bright colorful backgrounds which accentuate the glass effect. So, we wi
    2 min read
  • Animation in Android with Example
    Animation is the process of adding a motion effect to any view, image, or text. With the help of an animation, you can add motion or can change the shape of a specific view. Animation in Android is generally used to give your UI a rich look and feel. The animations are basically of three types as fo
    7 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
  • Explode Animation in Android
    In this article, we are going to show the android explode animation. This is something that is very interesting. You may have seen this feature in some game apps or even many money transfer apps. They show rewards like this. When you click on the reward it will explode and will show the reward that
    2 min read
  • How to make Heart Fill animation 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 VectorDrawablesAnimate the VectorDrawable such as fill color etc.Draw paths and do Path MorphingAn AnimatedVectorDrawa
    4 min read
  • Shimmer Animation in Android using Jetpack Compose
    Shimmer Animation was created by Facebook to show the loading screen while images are fetched from the server. Now we see shimmer animation in lots of places. In this article, we will take a look at the implementation of shimmer animation using the all-new Jetpack Compose. A sample GIF is given belo
    3 min read
  • BungeeAnimation in Android with Example
    BungeeAnimation is an animation library that helps to gain the attention of the user. As it is known that apps are made up of many activities so it is common for the user to travel between different activities in an app. By adding animation to those transactions it will surely attract users. Activit
    5 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
  • How to Apply View Animations Effects in Android?
    Android View Animations are used to apply amazing animations on TextView and EditText in the android application. Such animations provide the app with a smooth look in a new way. In this article, we are going to develop the Android View Animation effect in Android Studio. What we are going to build
    3 min read
  • 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences