How to Implement Fab Explosion Animation in Android?
Last Updated : 04 Jul, 2023
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.
Resource file for explosion animationAdd 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:
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