Skip to content

Instantly share code, notes, and snippets.

@muhammedesadcomert
Last active January 4, 2024 09:28
Show Gist options
  • Save muhammedesadcomert/41c1eed3b4a28fbd918154ec2c74fe17 to your computer and use it in GitHub Desktop.
Save muhammedesadcomert/41c1eed3b4a28fbd918154ec2c74fe17 to your computer and use it in GitHub Desktop.
Changing navigation bar color according to screen
@Composable
fun AppNavHost(navController: NavHostController) {
val context = LocalContext.current
val navigationBarColor = animateColorAsState(
targetValue = when (navController.currentDestination()?.route) {
Screen.ScreenOne.route -> color_one
Screen.ScreenTwo.route -> color_two
else -> default_color
},
label = "navigationBarColor",
)
context.changeNavigationBarColor(navigationBarColor.value.toArgb())
// Your screens...
}
@Composable
fun NavHostController.currentDestination(): NavDestination? {
val navBackStackEntry by currentBackStackEntryAsState()
return navBackStackEntry?.destination
}
fun Context.getActivity(): ComponentActivity? = when (this) {
is ComponentActivity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
}
fun Context.changeNavigationBarColor(color: Int) {
getActivity()?.window?.navigationBarColor = color
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment