Introduction
Bottom navigation is a widely recognized and utilized feature in Android applications, finding its place in virtually every app, from popular platforms like Instagram, Facebook, and Twitter to numerous others. These bottom navigation bars serve as a navigational tool, facilitating seamless movement between key destinations within an app. With the power of Jetpack Compose, creating a dynamic and visually engaging bottom navigation experience has never been more accessible. In this article, we are going to deep dive into the bottom navigation bars in Jetpack Compose.

Setup
Add the dependency in 'app/build.gradle'.
implementation "androidx.navigation:navigation-compose:2.6.0"
Consider adding the latest dependency of the navigation. You can the latest dependency here Navigation Repository.
Step 1. Define Destination
After performing sync in your project, create a separate file in the directory for the bottom navigation destination called 'Destinations'. Now create a sealed class with the name 'Destinations' with the bottom navigation item icon, title, and route, which we will use later for navigation between screens. This approach streamlines the management of navigation destinations while encapsulating relevant information for each destination.
sealed class Destinations(
val route: String,
val title: String? = null,
val icon: ImageVector? = null
) {
object HomeScreen : Destinations(
route = "home_screen",
title = "Home",
icon = Icons.Outlined.Home
)
object Favourite : Destinations(
route = "favourite_screen",
title = "Favorite",
icon = Icons.Outlined.Favorite
)
object Notification : Destinations(
route = "notification_screen",
title = "Notification",
icon = Icons.Outlined.Notifications
)
}
Step 2. Define screens to Display
To facilitate comprehension, I've outlined three screens in a file for illustrative purposes. However, you can easily differentiate between them. Presently, each screen solely features centered text content.
@Composable
fun HomeScreen() {
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.DarkGray)
.wrapContentSize(Alignment.Center)
) {
Text(
text = "Home Screen",
style = MaterialTheme.typography.titleLarge,
color = Color.White,
modifier = Modifier.align(Alignment.CenterHorizontally),
textAlign = TextAlign.Center,
)
}
}
@Composable
fun FavouriteScreen() {
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Magenta)
.wrapContentSize(Alignment.Center)
) {
Text(
text = "Favourite Screen",
style = MaterialTheme.typography.titleLarge,
color = Color.White,
modifier = Modifier.align(Alignment.CenterHorizontally),
textAlign = TextAlign.Center,
)
}
}
@Composable
fun NotificationScreen() {
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Blue)
.wrapContentSize(Alignment.Center)
) {
Text(
text = "Notification Screen",
style = MaterialTheme.typography.titleLarge,
color = Color.White,
modifier = Modifier.align(Alignment.CenterHorizontally),
textAlign = TextAlign.Center,
)
}
}
Step 3. Define Navigation Graph
Here we define a Composable function named NavigationGraph that defines the navigation structure within a Jetpack Compose app. It utilizes a NavHost to manage different destinations facilitated by a NavHostController. The starting destination is set to the route of the "HomeScreen." Within the NavHost, there are three composable blocks, each corresponding to a different screen destination: "HomeScreen," "FavouriteScreen," and "NotificationScreen." As users navigate through the app, the appropriate composable function, such as HomeScreen(), FavouriteScreen(), or NotificationScreen(), will be invoked to render the content of the respective screen. This architecture streamlines navigation and content presentation, ensuring a smooth user experience.
@Composable
fun NavigationGraph(navController: NavHostController) {
NavHost(navController, startDestination = Destinations.HomeScreen.route) {
composable(Destinations.HomeScreen.route) {
HomeScreen()
}
composable(Destinations.Favourite.route) {
FavouriteScreen()
}
composable(Destinations.Notification.route) {
NotificationScreen()
}
}
}
Step 4. Create Bottom Navigation with Material 3
The BottomBar Composable function orchestrates the construction of a bottom navigation bar in Jetpack Compose. With inputs including a navigation controller, a mutable state, and an optional modifier for styling, it assembles a set of navigation items corresponding to different screens in the app. Each navigation item incorporates a label and an icon. The currently active route is determined by examining the back stack, enabling proper highlighting of the selected item. When an item is clicked, the navigation controller facilitates seamless screen transitions.



CodefathermwPosted Jan 9, 2024, 8:04 PM
Landila ulemu wako @Ravikant