Monday, March 7, 2022

jetpack compose - How to change SystemBars color

MainActivity.kt

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 SystemBars Color")},
                backgroundColor = Color(0xFF8DB600)
            )},
            content = { MainContent() },
            backgroundColor = Color(0xFFBFAFB2)
        )
    }


    @Composable
    fun MainContent() {
        val systemUiController = rememberSystemUiController()
        systemUiController.setSystemBarsColor(Color.Blue)

        Box(Modifier.fillMaxSize().wrapContentSize(Alignment.Center)) {
            Button(onClick = {
                systemUiController.setSystemBarsColor(
                    color = Color.Red
                )
            }) {
                Text(text = "Set SystemBars Color Red")
            }
        }
    }
}
build.gradle[app] dependencies

implementation "com.google.accompanist:accompanist-systemuicontroller:0.17.0"