I am making a simple app in which I want that when I press a button the brightness of the device is set to maximum and when I press it again it goes back to how it was before. but I can't find anywhere on the internet how to do this, I've been trying several ways and I can't get it to work.
Asked
Active
Viewed 637 times
2
-
Does this answer your question? [Change the System Brightness Programmatically](https://stackoverflow.com/questions/18312609/change-the-system-brightness-programmatically) – Michael Celey Jun 11 '22 at 15:50
1 Answers
0
Here is how to change brightness
@Composable
fun UpdateBrightness() {
val context = LocalContext.current
DisposableEffect(Unit) {
setBrightness(context, isFull = true)
onDispose {
setBrightness(context, isFull = false)
}
}
}
fun setBrightness(context: Context, isFull: Boolean) {
val activity = context as? Activity ?: return
val layoutParams: WindowManager.LayoutParams = activity.window.attributes
layoutParams.screenBrightness = if (isFull) 1f else WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
activity.window.attributes = layoutParams
}
@Composable
fun MyScreen() {
UpdateBrightness() // Set maximum brightness by default
...
}
The code is taken from this article: https://medium.com/@yhdista/change-the-brightness-for-the-screen-thinking-in-compose-84378dce3b6c

Mikhail
- 2,612
- 3
- 22
- 37