Skip to main content

jetpack compose - BottomAppBar with FAB example

Compose BottomAppBar With FAB
The BottomAppBar is a useful android jetpack compose library widget. The BootomAppBar displays the bottom navigation items in its ‘content’ placeholder. BottomAppBar ‘content’ parameter has a Row scope, so items inside it display horizontally side by side. We can change the BottomAppBar default background color, content color, elevation, and content padding size. Even we can optionally embed a FloatingActionButton with the BottomAppBar widget.

The following android application development tutorial will demonstrate to us how we can embed a FloatingActionButton inside BottomAppBar in a kotlin jetpack compose application. To do that at first, we have to add a Scaffold widget into our composable function.

The Scaffold widget implements the basic material design visual layout structure in a jetpack compose application. Scaffold widget provides API to insert several material components to construct app screen such as TopAppBar, BottomAppBar, FloatingActionButton, etc. So that, we can show a FloatingActionButton in our jetpack compose application using the Scaffold ‘floatingActionButton’ parameter. Simply we can pass a FloatingActionButton instance to this parameter to show a floating action button on our mobile device screen.

We can define the FloatingActionButton screen position by using the Scaffold ‘floatingActionButtonPosition’ parameter. In this example kotlin code, we passed the ‘FabPosition.Center’ value to this parameter. So our floating action button displays in the bottom center of the mobile screen.

Now the question is how can we embed our FloatingActionButton with BottomAppBar? The answer is that the Scaffold has a parameter name ‘isFloatingActionButtonDocked’, when we set this parameter value to ‘true’, it embeds the FloatingActionButton with BottomAppBar. We also set the FAB position to center and we set the docked value to true, so the FloatingActionButton now shows in our BottomAppBar center position in embedded style. We also can show the FloatingActionButton at the position of the end/right of the BottomAppBar.

We can define the cutout shape for the BottomAppBar FloatingActionButton embedding style. The BottomAppBar ‘cutoutShape’ parameter allows us to specify the cutout shape for FlaotingActionButton such as circle shape, rounded corner shape, or rectangle shape.

Copy the following composable kotlin code into your android studio project and run it on your emulator device to see the output. We also add a screenshot of this app’s user interface at the bottom of the tutorial that will help you to understand the code without running it on your device.
MainActivity.kt

package com.cfsuman.jetpackcompose

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MainContent()
        }
    }

    @SuppressLint("UnusedMaterialScaffoldPaddingParameter")
    @Composable
    fun MainContent(){
        val result = remember { mutableStateOf("") }
        val selectedItem = remember { mutableStateOf("share")}
        val fabShape = RoundedCornerShape(50)

        Scaffold(
            topBar = {
                TopAppBar(
                    title = {
                        Text(text = "Bottom app bar + FAB")
                    },

                    navigationIcon = {
                        IconButton(
                            onClick = {
                                result.value = "Drawer icon clicked"
                            }
                        ) {
                            Icon(Icons.Filled.Menu, contentDescription = "")
                        }
                    },

                    backgroundColor = Color(0xFFFF5470),
                    elevation = AppBarDefaults.TopAppBarElevation
                )
            },

            content = {
                Box(
                    Modifier
                        .background(Color(0XFFE3DAC9))
                        .padding(16.dp)
                        .fillMaxSize(),
                ) {
                    Text(
                        text = result.value,
                        fontSize = 22.sp,
                        fontFamily = FontFamily.Serif,
                        modifier = Modifier.align(Alignment.Center)
                    )
                }
            },

            floatingActionButton = {
                FloatingActionButton(
                    onClick = {result.value = "FAB clicked"},
                    shape = fabShape,
                    backgroundColor = Color(0xFFFF8C00)
                ) {
                    Icon(Icons.Filled.Add,"")
                }
            },
            isFloatingActionButtonDocked = true,
            floatingActionButtonPosition = FabPosition.Center,

            bottomBar = {
                BottomAppBar(
                    cutoutShape = fabShape,
                    content = {
                        BottomNavigationItem(
                            icon = {
                                Icon(Icons.Filled.Favorite , "")
                            },
                            label = { Text(text = "Favorite")},
                            selected = selectedItem.value == "favorite",
                            onClick = {
                                result.value = "Favorite icon clicked"
                                selectedItem.value = "favorite"
                            },
                            alwaysShowLabel = false
                        )

                        BottomNavigationItem(
                            icon = {
                                Icon(Icons.Filled.Share ,  "")
                            },

                            label = { Text(text = "Share")},
                            selected = selectedItem.value == "share",
                            onClick = {
                                result.value = "Share icon clicked"
                                selectedItem.value = "share"
                            },
                            alwaysShowLabel = false
                        )
                    }
                )
            }
        )
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        //MainContent()
    }
}
More android jetpack compose tutorials

Popular posts from this blog

Restricting Jetpack Compose TextField to Numeric Input Only

Jetpack Compose has revolutionized Android development with its declarative approach, enabling developers to build modern, responsive UIs more efficiently. Among the many components provided by Compose, TextField is a critical building block for user input. However, ensuring that a TextField accepts only numeric input can pose challenges, especially when considering edge cases like empty fields, invalid characters, or localization nuances. In this blog post, we'll explore how to restrict a Jetpack Compose TextField to numeric input only, discussing both basic and advanced implementations. Why Restricting Input Matters Restricting user input to numeric values is a common requirement in apps dealing with forms, payment entries, age verifications, or any data where only numbers are valid. Properly validating input at the UI level enhances user experience, reduces backend validation overhead, and minimizes errors during data processing. Compose provides the flexibility to implement ...

jetpack compose - TextField remove underline

Compose TextField Remove Underline The TextField is the text input widget of android jetpack compose library. TextField is an equivalent widget of the android view system’s EditText widget. TextField is used to enter and modify text. The following jetpack compose tutorial will demonstrate to us how we can remove (actually hide) the underline from a TextField widget in an android application. We have to apply a simple trick to remove (hide) the underline from the TextField. The TextField constructor’s ‘colors’ argument allows us to set or change colors for TextField’s various components such as text color, cursor color, label color, error color, background color, focused and unfocused indicator color, etc. Jetpack developers can pass a TextFieldDefaults.textFieldColors() function with arguments value for the TextField ‘colors’ argument. There are many arguments for this ‘TextFieldDefaults.textFieldColors()’function such as textColor, disabledTextColor, backgroundColor, cursorC...

jetpack compose - Image clickable

Compose Image Clickable The Image widget allows android developers to display an image object to the app user interface using the jetpack compose library. Android app developers can show image objects to the Image widget from various sources such as painter resources, vector resources, bitmap, etc. Image is a very essential component of the jetpack compose library. Android app developers can change many properties of an Image widget by its modifiers such as size, shape, etc. We also can specify the Image object scaling algorithm, content description, etc. But how can we set a click event to an Image widget in a jetpack compose application? There is no built-in property/parameter/argument to set up an onClick event directly to the Image widget. This android application development tutorial will demonstrate to us how we can add a click event to the Image widget and make it clickable. Click event of a widget allow app users to execute a task such as showing a toast message by cli...