Floating Action Button (FAB) in Android with Example
Last Updated :
13 Feb, 2025
The floating action button is a bit different button from the ordinary buttons. Floating action buttons are implemented in the app’s UI for primary actions (promoted actions) for the users and the actions under the floating action button are prioritized by the developer. For example the actions like adding an item to the existing list. So in this article, it has been shown how to implement the Floating Action Button (FAB), and also the buttons under the FABs are handled with a simple Toast message.
Note that we are going to implement this project using Java/Kotlin language.
Types of Floating Action Button
There are mainly four types of floating action buttons available on Android.
- Normal/Regular Floating Action Button
- Mini Floating Action Button
- Extended Floating Action Button
- Theming Floating Action Button
In this article let’s discuss the Normal/Regular Floating Action Button with a sample example in Android.
Normal/Regular Floating Action Button
Regular FABs are FABs that are not expanded and are regular size. The following example shows a regular FAB with a settings icon.

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.
The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Adding Dependency to the build.gradle File
Go to Module build.gradle.kts file and add this dependency and click on Sync Now button.
implementation ("com.google.android.material:material:1.12.0")
Step 3: Add a vector asset to the Drawable File
For demonstration purposes will import 3 icons in the Drawable folder, and one can import the icons of his/her choice. One can do that by right-clicking the drawable folder > New > Vector Asset. Refer to the following image to import the vector Icon.

Now select your vector icon

Step 4: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
In the activity_main.xml file add the floating action buttons and invoke the following code. Now invoke the normal FAB. Which is of 56dp radius. We have chained the sub-FABs to the parent FABs so that they are in a single key line. Comments are added inside the code to understand the code in more detail.
XML
<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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<!-- This will be the parent Floating Action Button -->
<!-- After the implementation the Floating Action Button
at the bottom right corner -->
<!-- After clicking the above button the following two buttons
will pop up. So this button is considered as parent FAB -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/button_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="@drawable/settings"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!-- Floating action button for home -->
<!-- Make sure that you are constraining this
button to the parent button -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/button_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/button_settings"
app:layout_constraintEnd_toEndOf="@+id/button_settings"
app:layout_constraintStart_toStartOf="@+id/button_settings"
app:srcCompat="@drawable/home" />
<!-- Action name text for the home button -->
<!-- Make sure that you are constraining this Text to
the Home FAB button -->
<TextView
android:id="@+id/text_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Go to home"
app:layout_constraintBottom_toBottomOf="@+id/button_home"
app:layout_constraintEnd_toStartOf="@+id/button_home"
app:layout_constraintTop_toTopOf="@+id/button_home" />
<!-- Floating action button for profile -->
<!-- Make sure that you are constraining this
button to the Home FAB button -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/button_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/button_home"
app:layout_constraintEnd_toEndOf="@+id/button_home"
app:layout_constraintStart_toStartOf="@+id/button_home"
app:srcCompat="@drawable/person" />
<!-- Action name text for the profile button -->
<!-- Make sure that you are constraining this Text
to the Profile FAB button -->
<TextView
android:id="@+id/text_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Go to Profile"
app:layout_constraintBottom_toBottomOf="@+id/button_profile"
app:layout_constraintEnd_toStartOf="@+id/button_profile"
app:layout_constraintTop_toTopOf="@+id/button_profile" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output UI:

Step 5: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. Now, we handle all FABs using the setOnClickListener() method you may refer to Handling Click events in Button in Android.
In this code, it’s been shown that when sub FABs are to be visible with onClickListener. Comments are added inside the code to understand the code in more detail.
MainActivity File:
MainActivity.java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
// Declare Floating Action Buttons (FABs)
private FloatingActionButton settings;
private FloatingActionButton home;
private FloatingActionButton profile;
// Declare TextViews for FAB labels
private TextView homeTextview;
private TextView profileTextview;
// Variable to track visibility of sub FABs
private boolean areAllButtonsEnabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the parent FAB (Settings)
settings = findViewById(R.id.button_settings);
// Initialize sub FABs (Home & Profile)
home = findViewById(R.id.button_home);
profile = findViewById(R.id.button_profile);
// Initialize TextViews for sub FABs
homeTextview = findViewById(R.id.text_home);
profileTextview = findViewById(R.id.text_profile);
// Initially hide sub FABs and labels
home.setVisibility(View.GONE);
profile.setVisibility(View.GONE);
homeTextview.setVisibility(View.GONE);
profileTextview.setVisibility(View.GONE);
// Set default state to false (sub FABs are hidden)
areAllButtonsEnabled = false;
// Set click listener for the parent FAB
settings.setOnClickListener(view -> {
if (!areAllButtonsEnabled) {
// Show sub FABs and labels
home.show();
profile.show();
homeTextview.setVisibility(View.VISIBLE);
profileTextview.setVisibility(View.VISIBLE);
// Update the state
areAllButtonsEnabled = true;
} else {
// Hide sub FABs and labels
home.hide();
profile.hide();
homeTextview.setVisibility(View.GONE);
profileTextview.setVisibility(View.GONE);
// Update the state
areAllButtonsEnabled = false;
}
});
// Set click listener for the Profile FAB
profile.setOnClickListener(view ->
Toast.makeText(MainActivity.this, "Switched to profile", Toast.LENGTH_SHORT).show()
);
// Set click listener for the Home FAB
home.setOnClickListener(view ->
Toast.makeText(MainActivity.this, "Switched to Home", Toast.LENGTH_SHORT).show()
);
}
}
MainActivity.kt
package org.geeksforgeeks.demo
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private lateinit var settings: FloatingActionButton
private lateinit var home: FloatingActionButton
private lateinit var profile: FloatingActionButton
// These are taken to make visible and invisible along with FABs
private lateinit var homeTextview: TextView
private lateinit var profileTextview: TextView
// to check whether sub FAB buttons are visible or not.
private var areAllButtonsEnabled: Boolean? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Register all the FABs with their IDs This FAB button is the Parent
settings = findViewById(R.id.button_settings)
// FAB button
home = findViewById(R.id.button_home)
profile = findViewById(R.id.button_profile)
// Also register the action name text, of all the FABs.
homeTextview = findViewById(R.id.text_home)
profileTextview = findViewById(R.id.text_profile)
// Now set all the FABs and all the action name texts as GONE
home.visibility = View.GONE
profile.visibility = View.GONE
homeTextview.visibility = View.GONE
profileTextview.visibility = View.GONE
// make the boolean variable as false, as all the
// action name texts and all the sub FABs are invisible
areAllButtonsEnabled = false
// We will make all the FABs and action name texts
// visible only when Parent FAB button is clicked So
// we have to handle the Parent FAB button first, by
// using setOnClickListener you can see below
settings.setOnClickListener(View.OnClickListener {
(if (!areAllButtonsEnabled!!) {
// when areAllButtonsEnabled becomes true make all
// the action name texts and FABs VISIBLE
home.show()
profile.show()
homeTextview.visibility = View.VISIBLE
profileTextview.visibility = View.VISIBLE
// make the boolean variable true as we
// have set the sub FABs visibility to GONE
true
} else {
// when areAllButtonsEnabled becomes true make
// all the action name texts and FABs GONE.
home.hide()
profile.hide()
homeTextview.visibility = View.GONE
profileTextview.visibility = View.GONE
// make the boolean variable false as we
// have set the sub FABs visibility to GONE
false
}).also { areAllButtonsEnabled = it }
})
// below is the sample action to handle profile FAB. Here it shows simple Toast msg.
// The Toast will be shown only when they are visible and only when user clicks on them
profile.setOnClickListener {
Toast.makeText(this, "Switched to profile", Toast.LENGTH_SHORT).show()
}
// below is the sample action to handle home FAB. Here it shows simple Toast msg
// The Toast will be shown only when they are visible and only when user clicks on them
home.setOnClickListener {
Toast.makeText(this, "Switched to Home", Toast.LENGTH_SHORT).show()
}
}
}
Output:
Similar Reads
Extended Floating Action Button in Android with Example
In the Floating Action Button (FAB) article, we have discussed on Normal/Regular Floating Action Button and Mini Floating Action Button. In this article, let's discuss and implement an Extended Floating Action Button in Android which extends when clicked and shrinks when closed and also shows the in
10 min read
Counter Floating Action Button in Android with Example
Counter Floating Action button is seen in most of the E-commerce apps where we are using the functionality to display the number of items that are present in the user's shopping cart. In this article, we will take a look at implementing this counter floating action button in our Android app in Andro
3 min read
Theming Floating Action Buttons in Android with Example
Prerequisite: Floating Action Button (FAB) in Android with ExampleExtended Floating Action Button in Android with Example Android application developers want to seek the attention of the users by customizing and theming the android application widgets and keep more traffic of customers only by the d
12 min read
Android Floating Action Button in Kotlin
Floating action buttons are used in android applications to indicate the user for some priority-based task. Generally floating action buttons in the android applications are found aligned to the bottom end of the application. In this article, we will take a look at How to implement the Floating Acti
4 min read
Floating Action Button using Fab Option Library in Android
Floating Action Button using Fab Options is another unique way of displaying various options. With the help of this, we can Navigate to different screens easily. This Floating Action button display various menu with Animation. So it increases user experience. In this article, we are going to learn h
3 min read
Floating Action Button in Android using Jetpack Compose
Floating Action Button is added to the android application to perform some important within the android application. These are implemented in the android application UI for some primary actions within the application. There are different types of floating action buttons such as simple, square, and e
3 min read
How to Change Colors of a Floating Action Button in Android?
Android applications use the Floating Action Button for prompting the user to perform some important action within the android application. Floating Action Buttons in android applications are used to perform some important functionality within android applications. Many times in the android applicat
3 min read
How to Add Image on Floating Action Button in Android?
A floating action button (FAB) is a user interface element in the mobile application that is typically circular and floats above the main content. It usually has a prominent color and icon, and it is used to provide quick access to the most commonly used action within the app. Step-by-Step Implement
1 min read
How to change Input Method Action Button in Android?
In this article, IME(Input Method Action) Option is changed in android according to our requirement. Input Method Action Button is located in the bottom right corner of the soft keyboard. By default, the system uses this button for either a Next or Done action unless your text field allows multi-lin
2 min read
Theming Floating Action Button with Bottom Navigation Bar in Android
In the previous article How to Add a Floating Action Button to Bottom Navigation Bar in Android?, it's well discussed how to add the Floating Action Button to the Bottom Navigation Bar in Android. Now to increase the UI/UX experience theming also can be done for the Bottom Navigation Bar. So in this
4 min read