Jetpack Compose Floating Action Button

The Floating Action Button (FAB) is a prominent UI element that represents a primary action in an app. It floats above the content and provides easy access to the most important functionality. The FAB is often used for actions like adding a new item, composing a message, or initiating a crucial operation.

To create a Floating Action Button in Jetpack Compose, we utilize the FloatingActionButton component from the Material Design library. And in this post, we will use the Material 3 library.

Key Points about FAB:

  • FAB should be used for the most common or important action on a screen.
  • Icons in a FAB must be clear and understandable because these buttons do not have a text label.
  • The FAB should persist on the screen when content is scrolling.

Floating Action buttons in jetpack compose have three sizes:

  1. FAB
  2. Small FAB
  3. Large FAB

1. Floating Action Button

 FloatingActionButton(onClick = { /*TODO*/ }) {
            IconButton(onClick = { /*TODO*/ }) {
                Icon(imageVector = Icons.Outlined.Edit, contentDescription = null)
            }
        }
floating action button

FloatingActionButton function has many parameters but only two are compulsory and others are optional. Compulsory are:

  • onClick: It is called when this FAB is clicked
  • content: This parameter defines a lambda that contains the content of this FAB, typically an Icon.

Customizing the FAB

Jetpack Compose provides several customization options for the Floating Action Button. We can modify its appearance, position, and behaviour to align with our app’s design language. Some common customizations include:

  • shape
  • elevation
  • container color
  • content color

Let’s see them with an example:

1. Shape

Note: You can use the modifier to manipulate the position of the floating action button. We will not discuss modifiers here. Check out our post on modifiers if you are not aware of them.

1. Circle
FloatingActionButton(
            onClick = { /*TODO*/ },
            shape = CircleShape
        ){...} // code is same as above in curly brackets
 floating action button in a circular shape
2. Rectangle
FloatingActionButton(
            onClick = { /*TODO*/ },
            shape = RectangleShape
        ){...} // code is same as above in curly brackets
floating action button in a rectangular shape

2. Color

In this section, we will change the color of fab and its content.

1. Container color

This parameter of FloatingActionButton function sets color to the background of the FAB.

FloatingActionButton(
            onClick = { /*TODO*/ },
            containerColor = Color.Yellow
        ){...}
floating action button in a yellow color
2. Content Color

This parameter sets the color to the content of the FAB

FloatingActionButton(
            onClick = { /*TODO*/ },
            containerColor = Color.Yellow,
            contentColor = Color.Magenta
        ){...}
jetpack compose floating action button in a yellow color with content of fab in magenta color

3. Elevation

This parameter is used to resolve the elevation for FAB in different states. This controls the size of the shadow below the FAB.

FloatingActionButton(
            onClick = { /*TODO*/ },
            elevation = FloatingActionButtonDefaults.elevation(16.dp)
        )
simple floating action button
Without elevation
an elevated floating action button of jetpack compose
with an elevation of 16 dp
image showing a floating action button with a elevation of 48 dp
with an elevation of 48 dp

2. Small Floating Action Button

SmallFloatingActionButton(onClick = { /*TODO*/ },) {
            IconButton(onClick = { /*TODO*/ }) {
                Icon(imageVector = Icons.Outlined.Edit, 
                        contentDescription = null)
            }
        }
a small floating action button
Small FAB
simple floating action button
Simple FAB

3. Large Floating Action Button

LargeFloatingActionButton(onClick = { /*TODO*/ },) {
            IconButton(onClick = { /*TODO*/ }) {
                Icon(imageVector = Icons.Outlined.Edit, 
                            contentDescription = null)
            }
        }
a large floating action button
Large FAB
a simple floating action button
Simple FAB

Note: For styling of these small and large floating action buttons, please refer to the styling of simple floating action as all parameters are the same in all the FABs.

The Floating Action Button is a crucial component in modern Android app design, and with Jetpack Compose, creating and customizing FABs has become more straightforward.

Some Useful links

Scaffold

Material FAB

Happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *