Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The Floating Action Button (FAB) is a circular button that floats above the UI and is used for a primary action in the application, like adding a new item or composing a message. The FAB is a part of the Material Design guidelines.
Here's a guide on how to implement a Floating Action Button in an Android app using the AndroidX library:
Ensure you have the Material Design library dependency in your app's build.gradle
:
implementation 'com.google.android.material:material:1.4.0'
In your XML layout, you can add the FAB as:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:contentDescription="@string/fab_description" app:srcCompat="@drawable/ic_add_white_24dp" />
Here, ic_add_white_24dp
is a sample drawable icon representing the "add" action. You'd want to replace this with your own icon, based on the action your FAB will perform.
In your activity or fragment, you can set a click listener on the FAB:
FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Handle FAB click Snackbar.make(view, "FAB Clicked", Snackbar.LENGTH_LONG).show(); } });
In the example above, a Snackbar
is shown when the FAB is clicked, but you can replace it with your own desired action.
The Material Design library also provides an Extended FAB, which is a wider FAB that can contain text alongside the icon:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton android:id="@+id/extended_fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/extended_fab_description" android:icon="@drawable/ic_add_white_24dp" android:text="@string/add_item"/>
The handling of click events for the Extended FAB is identical to the regular FAB.
You can customize the FAB's appearance (e.g., background color, ripple color) using various attributes or by defining a custom style.
Place the FAB in a location that doesn't obstruct important information or actions. Generally, it's placed at the bottom-right corner of the screen, but depending on your app's layout and content, you might want to adjust its position.
The FAB should represent the primary or most common action on a screen. Avoid using multiple FABs on the same screen, as it can confuse the users about the primary action.
That's the basic introduction to FABs in Android. Depending on your requirements, you might also want to look into behaviors like hiding the FAB when scrolling or animating it based on certain actions.
Adding Floating Action Button in Android example code:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" android:layout_gravity="bottom|end" android:layout_margin="16dp"/>
Customizing Floating Action Button appearance in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/customFab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="@color/customColor" app:srcCompat="@drawable/custom_icon"/>
Handling click events on Floating Action Button:
FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Handle FAB click event } });
Animations for Floating Action Button in Android:
FloatingActionButton fab = findViewById(R.id.fab); // Scale animation fab.animate().scaleX(1.5f).scaleY(1.5f).setDuration(300); // Rotation animation fab.animate().rotation(45).setDuration(300);
Positioning Floating Action Button on screen in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp"/>
Using extended FAB in Android with example code:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton android:id="@+id/extendedFab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Extended FAB"/>
Menu options with Floating Action Button in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/ic_add" app:useCompatPadding="true" android:layout_gravity="bottom|end" android:layout_margin="16dp"/>
Implementing actions and behaviors with FAB in Android:
FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Perform action when FAB is clicked } });