Jetpack Compose - Hiding the Status Bar Programmatically
This code demonstrates how to hide the status bar in a Jetpack Compose application. It utilizes the accompanist-systemuicontroller
library to achieve this functionality.
The code is structured within a MainActivity
class extending ComponentActivity
. Let's break down the key functionalities:
Setting Up the Content:
- The
onCreate
method sets the content of the activity usingsetContent
. This function takes a composable function that defines the UI hierarchy. In this case, it calls theGetScaffold
composable.
- The
Scaffold for Application Structure:
GetScaffold
defines the overall application structure using aScaffold
composable. This provides a standard layout with optional elements like a top bar, content area, and background color.
Top Bar for Title:
- The
topBar
property ofScaffold
defines the top app bar usingTopAppBar
. This displays the app title ("Compose - How To Hide StatusBar") and sets its background color.
- The
Content Area:
- The
content
property defines the main content area using theMainContent
composable.
- The
Hiding the Status Bar:
- Inside
MainContent
, therememberSystemUiController
function retrieves a reference to the system UI controller. - By setting
systemUiController.isStatusBarVisible
tofalse
, the code programmatically hides the status bar for the current screen.
- Inside
Background Color:
- The
Scaffold
and the content area (MainContent
) set their own background colors using theColor
composable from Jetpack Compose's material library.
- The
Dependency:
- The
build.gradle
file ensures the necessary library (accompanist-systemuicontroller
) is included in the project's dependencies.
- The
Summary
This example effectively demonstrates hiding the status bar in Jetpack Compose using the accompanist-systemuicontroller
library. The code leverages composables like Scaffold
, TopAppBar
, and MainContent
to structure the UI and employs the system UI controller to manipulate the status bar visibility. Remember, hiding the status bar might impact user experience, so use it judiciously in your applications.
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 StatusBar")},
backgroundColor = Color(0xFF91A3B0)
)},
content = { MainContent() },
backgroundColor = Color(0xFFA9B2C3)
)
}
@Composable
fun MainContent() {
val systemUiController = rememberSystemUiController()
// hide status bar programmatically
systemUiController.isStatusBarVisible = false
}
}
implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"
- jetpack compose - TopAppBar center title
- jetpack compose - Snackbar action
- jetpack compose - How to use AndroidView
- jetpack compose - How to use navigation controller
- jetpack compose - Room add remove update
- jetpack compose - Background brush
- jetpack compose - Combined clickable
- jetpack compose - How to use TabRow
- jetpack compose - TabRow indicator color
- jetpack compose - Get screen width height in dp
- jetpack compose - Get screen layout direction
- jetpack compose - How to hide navigation bar
- jetpack compose - How to hide system bars
- jetpack compose - Detect screen orientation change
- jetpack compose ktor - How to get api data