Skip to content

Instantly share code, notes, and snippets.

@vengateshm
Created September 29, 2023 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vengateshm/76287321edc12c7dd0fc539021a74156 to your computer and use it in GitHub Desktop.
Save vengateshm/76287321edc12c7dd0fc539021a74156 to your computer and use it in GitHub Desktop.
This code shows how to change status bar color and navigation bar color in jetpack compose.
import android.app.Activity
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.delay
class SystemBarColorChangeActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
val colors = remember {
listOf(
Color(0xFFFFC400),
Color(0xFFFF9100),
Color(0xFFFF3D00),
)
}
var colorIndex by remember { mutableIntStateOf(0) }
LaunchedEffect(Unit) {
colorIndex += 1
}
val animatedColor by animateColorAsState(
targetValue = colors[colorIndex],
animationSpec = tween(1500),
label = "status bar color animation"
)
val window = (LocalContext.current as Activity).window!!
LaunchedEffect(colorIndex) {
window.statusBarColor = animatedColor.toArgb()
window.navigationBarColor = animatedColor.toArgb()
delay(1500)
if (colorIndex < colors.lastIndex) {
colorIndex += 1
} else {
colorIndex = 0
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment