Search code examples
androidandroid-jetpack-composeandroid-jetpack

Use activity window.attributes in Jetpack Compose to change screenBrightness


I am trying to modify screenBrightness through update this:

   window.attributes = window.attributes.apply { screenBrightness = 0.2f }

But I can get window in MainActivity : ComponentActivity() only. How to access is from Composable component?


Solution

  • I am using this to modify screen brightness in Jetpack Compose

    @Composable
    fun ComposableComponent() {
      val activity = LocalView.current.context as? Activity
      if (activity != null) {
        activity.window?.attributes = activity.window.attributes.apply {
          screenBrightness = -1.0f
        }
      }
    }