Jetpack Compose - Changing NavigationBar Color
This code demonstrates how to modify the NavigationBar color in a Jetpack Compose application. It utilizes the accompanist-systemuicontroller
library from Google to access and manipulate the system UI elements.
Code Breakdown:
Dependencies: The
build.gradle
snippet specifies the dependency onaccompanist-systemuicontroller
library. This library provides functionalities for interacting with the system UI, including the NavigationBar.MainActivity: This class represents the main activity of the application. It sets the content of the screen using
setContent
function within theonCreate
method.GetScaffold: This Composable function defines the overall layout structure of the app using the
Scaffold
composable. It includes aTopAppBar
with a title and a background color, the main content (MainContent
), and a background color for the entire screen.MainContent: This Composable builds the central content area. Here's what happens:
rememberSystemUiController
: This line retrieves a reference to theSystemUiController
instance, allowing interaction with system UI elements.systemUiController.setNavigationBarColor(Color.Green)
: This line sets the initial color of the NavigationBar to green.Box
: This container lays out its child composables (Button
) centered within the available space.Button
: This button triggers an action when clicked. Clicking the button callssystemUiController.setNavigationBarColor(Color.Blue)
, which changes the NavigationBar color to blue.
Summary:
This example showcases how to dynamically control the NavigationBar color in Jetpack Compose. By leveraging the accompanist-systemuicontroller
library, developers can customize the system UI elements to enhance the app's visual appearance and user experience.
package com.cfsuman.jetpackcompose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.google.accompanist.systemuicontroller.rememberSystemUiController
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {GetScaffold()}
}
@Composable
fun GetScaffold() {
Scaffold(
topBar = {TopAppBar(
title = {Text(text = "Compose - Set NavigationBar Color")},
backgroundColor = Color(0xFF5F9EA0),
)},
content = { MainContent() },
backgroundColor = Color(0xFFEFDECD)
)
}
@Composable
fun MainContent() {
val systemUiController = rememberSystemUiController()
systemUiController.setNavigationBarColor(Color.Green)
Box(Modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
Button(onClick = {
systemUiController.setNavigationBarColor(
color = Color.Blue
)
}) {
Text(text = "Set NavigationBar Color Blue")
}
}
}
}
implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
- jetpack compose - Get screen width height in dp
- jetpack compose - Get screen layout direction
- jetpack compose - How to change StatusBar color
- jetpack compose - How to change SystemBars color
- jetpack compose - How to hide status bar
- jetpack compose ktor - How to get api data
- jetpack compose - Get text from url
- jetpack compose - Kotlinx serialization lenient parsing
- jetpack compose - Kotlinx serialization ignore unknown keys
- jetpack compose - Kotlinx serialization encode to string
- jetpack compose - Kotlinx serialization decode from string
- jetpack compose - Kotlinx serialization build json element
- jetpack compose - Kotlinx serialization build json array
- jetpack compose - Random number flow
- jetpack compose - IconButton from vector resource