Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Jetpack Compose BottomNavBar? How can use bottomNavbar any Example?

I am working on jectpack compose in android application. So i want to use use bottomAppbar. Never found any example can someone help?

like image 776
affan ahmad Avatar asked Oct 26 '19 20:10

affan ahmad


Video Answer


2 Answers

With 1.0.x you can use the BottomAppBar

BottomAppBar (backgroundColor = yellow500) {
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Menu,"")
    }
    // The actions should be at the end of the BottomAppBar
    Spacer(Modifier.weight(1f, true))
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Favorite,"")
    }
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Call,"")
    }
}

enter image description here

With a FAB:

val fabShape = CircleShape
val scaffoldState = rememberScaffoldState()
val scope = rememberCoroutineScope()

Scaffold(
    scaffoldState = scaffoldState,
    drawerContent = { Text("Drawer content") },
    topBar = { },
    bottomBar = {
        BottomAppBar(cutoutShape = fabShape) {
            IconButton(
                onClick = {
                  scope.launch { scaffoldState.drawerState.open() }
                }
            ) {
                Icon(Icons.Filled.Menu,"")
            }
        }
    },
    floatingActionButtonPosition = FabPosition.Center,
    isFloatingActionButtonDocked = true,
    floatingActionButton = {
        FloatingActionButton(
            shape = fabShape,
            onClick = {}
        ) {
            Icon(Icons.Filled.Add,"")
        }
    }, content = {
        //bodyContent()
    })

enter image description here

like image 80
Gabriele Mariotti Avatar answered Nov 07 '22 18:11

Gabriele Mariotti


Yes jetpack compose support BottomAppBar with simple and FloatingActionButton Check below code example which help you more

BottomAppBarWithoutFab

@Composable
fun BottomAppBarNoFab(getMyActionImage: () -> Image, getMyNavigationImage: () -> Image) {
    val someActionImage: Image = getMyActionImage()
    val someNavigationImage: Image = getMyNavigationImage()
    val navigationIcon: @Composable() () -> Unit = {
        AppBarIcon(someNavigationImage) { /* doSomething()*/ }
    }
    val actionData = listOf(someActionImage)
    BottomAppBar(
        navigationIcon = navigationIcon,
        actionData = actionData
    ) { actionImage ->
        AppBarIcon(actionImage) { /* doSomething()*/ }
    }
}

Check BottomAppbarWithoutFab Screenshot

enter image description here

BottomAppBarWithCutout

@Composable
fun BottomAppBarCutoutFab(
    getMyActionImage: () -> Image,
    getMyNavigationImage: () -> Image
) {
    val someActionImage: Image = getMyActionImage()
    val someNavigationImage: Image = getMyNavigationImage()
    val navigationIcon: @Composable() () -> Unit = {
        AppBarIcon(someNavigationImage) { /* doSomething()*/}
    }
    val actionData = listOf(someActionImage)
    BottomAppBar(
        navigationIcon = navigationIcon,
        fabConfiguration = BottomAppBar.FabConfiguration(cutoutShape = CircleShape) {
            FloatingActionButton(
                color = +themeColor { secondary },
                icon = +imageResource(R.drawable.ic_add_icon),
                onClick = { /** doSomething() */ })
        },
        actionData = actionData
    ) { actionImage ->
        AppBarIcon(actionImage) { /* doSomething()*/  }
    }
}

Check bottomAppbarcutoutFab screenshot

enter image description here

Check below code how we call in @Compose function

Column(mainAxisAlignment = MainAxisAlignment.End) {
        BottomAppBarNoFab(getMyActionImage = {
            +imageResource(R.drawable.ic_home_icon)
        }, getMyNavigationImage = {
            +imageResource(R.drawable.ic_heart_icon)
        })
    }
like image 36
Anas Mehar Avatar answered Nov 07 '22 19:11

Anas Mehar