Jetpack Compose - Hiding the Navigation Bar
This code demonstrates how to hide the navigation bar programmatically in a Jetpack Compose application. It utilizes the accompanist-systemuicontroller
library to interact with the system UI elements.
Breakdown:
MainActivity: This is the entry point of the application. It sets the content of the activity using
setContent
with theGetScaffold
composable function.GetScaffold: This composable builds the main layout structure using a
Scaffold
element.topBar
: Defines the top app bar with title and styling.content
: Defines the content area of the screen, which in this case is filled by theMainContent
composable.backgroundColor
: Sets the background color for the entire scaffold.
MainContent: This composable retrieves a reference to the
SystemUiController
usingrememberSystemUiController
. It then directly sets theisNavigationBarVisible
property of the controller tofalse
to hide the navigation bar.build.gradle: The dependency for
accompanist-systemuicontroller
library is included with version 0.17.0 specified.
Summary
This code snippet provides a simple yet effective way to hide the navigation bar in a Jetpack Compose app. By leveraging the accompanist-systemuicontroller
library, you gain programmatic control over the system UI elements, allowing for a more immersive user experience when needed. Remember to consider the trade-offs of hiding the navigation bar, as users might expect it to be accessible.
package com.cfsuman.jetpackcompose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
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 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 - Hide NavigationBar")},
backgroundColor = Color(0xFF4B5320),
contentColor = Color.White
)},
content = { MainContent() },
backgroundColor = Color(0xFFEFBBCC)
)
}
@Composable
fun MainContent() {
val systemUiController = rememberSystemUiController()
// hide navigation bar programmatically
systemUiController.isNavigationBarVisible = false
}
}
implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
- jetpack compose - How to hide status bar
- jetpack compose - How to hide system bars
- jetpack compose - Detect screen orientation change
- jetpack compose ktor - How to get api data
- jetpack compose ktor - How to post data
- jetpack compose - Kotlinx serialization pretty print
- jetpack compose - Kotlinx serialization lenient parsing
- jetpack compose - Kotlinx serialization allow special floating point values
- jetpack compose - Kotlinx serialization build json element
- jetpack compose - Icon from vector resource