Wednesday, March 10, 2021

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()
    }
}