Jetpack Compose - Hiding System Bars
This code demonstrates how to hide the system bars (status bar and navigation bar) in a Jetpack Compose application. It achieves this by leveraging the accompanist-systemuicontroller
library.
Code Breakdown:
- Imports: The code imports necessary classes for building the UI and accessing the system UI controller.
- MainActivity: This is the main activity class that extends
ComponentActivity
. It sets the content of the activity usingsetContent
duringonCreate
. - GetScaffold: This composable function builds the main layout structure using
Scaffold
. It defines three sections:topBar
: This defines the top bar usingTopAppBar
with a title, background color, and content color.content
: This section displays the main content of the app, represented by theMainContent
composable.backgroundColor
: This sets the background color for the entire scaffold.
- MainContent: This composable retrieves the
SystemUiController
usingrememberSystemUiController
and then setsisSystemBarsVisible
tofalse
to hide both the status and navigation bars.
Summary:
This example showcases a simple yet effective approach to hiding system bars in Jetpack Compose. By utilizing the accompanist-systemuicontroller
library, the code achieves a clean separation of UI logic and system bar management. Remember, depending on your app's needs, you might want to consider showing or hiding the system bars conditionally.
MainActivity.kt
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 - How To Hide System Bars")},
backgroundColor = Color(0xFF592720),
contentColor = Color.White
)},
content = { MainContent() },
backgroundColor = Color(0xFFC2B280)
)
}
@Composable
fun MainContent() {
val systemUiController = rememberSystemUiController()
// hide system bars (status bar + navigation bar) programmatically
systemUiController.isSystemBarsVisible = false
}
}
build.gradle[app] dependencies
implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
- jetpack compose - How to hide status bar
- jetpack compose - How to hide navigation bar
- jetpack compose - Detect screen orientation change
- jetpack compose ktor - How to get api data
- jetpack compose - flowOf flow builder
- jetpack compose - Convert list to flow
- jetpack compose - Count down flow
- jetpack compose - Count up flow by ViewModel
- jetpack compose flow - Room add remove update
- jetpack compose flow - Room implement search
- jetpack compose - ViewModel Room edit update data
- jetpack compose - ViewModel Room delete clear data
- compose glance - How to create app widget
- jetpack compose - Icon from vector resource
- jetpack compose - IconButton from vector resource