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

Floating Action Button (FAB) in Android with Example

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:

1. Add Dependency:

Ensure you have the Material Design library dependency in your app's build.gradle:

implementation 'com.google.android.material:material:1.4.0'

2. Add the FAB to your Layout:

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.

3. Handle Click Events:

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.

4. (Optional) Extended FAB:

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.

5. Customizing the FAB:

You can customize the FAB's appearance (e.g., background color, ripple color) using various attributes or by defining a custom style.

Important Consideration:

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.

  1. Adding Floating Action Button in Android example code:

    • Description: Demonstrates how to add a Floating Action Button (FAB) to an Android layout.
    • 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"/>
      
  2. Customizing Floating Action Button appearance in Android:

    • Description: Shows how to customize the appearance of the Floating Action Button, such as changing its background color and icon.
    • Code:
      <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"/>
      
  3. Handling click events on Floating Action Button:

    • Description: Illustrates how to handle click events when the Floating Action Button is pressed.
    • Code:
      FloatingActionButton fab = findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              // Handle FAB click event
          }
      });
      
  4. Animations for Floating Action Button in Android:

    • Description: Introduces animations for the Floating Action Button, such as scale and rotation animations.
    • Code:
      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);
      
  5. Positioning Floating Action Button on screen in Android:

    • Description: Discusses different ways to position the Floating Action Button on the screen.
    • Code:
      <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"/>
      
  6. Using extended FAB in Android with example code:

    • Description: Introduces the Extended Floating Action Button, an extended version of the FAB with additional space for text and more content.
    • 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"/>
      
  7. Menu options with Floating Action Button in Android:

    • Description: Demonstrates how to add menu options to the Floating Action Button, creating a menu on click.
    • Code:
      <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"/>
      
  8. Implementing actions and behaviors with FAB in Android:

    • Description: Explores different actions and behaviors that can be associated with the Floating Action Button, such as opening a new activity or performing an action.
    • Code:
      FloatingActionButton fab = findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              // Perform action when FAB is clicked
          }
      });