Search code examples
androidseekbarbrightness

why my seek bar brightness setting locked for change and how can i fix it


I used some code to change brightness with custom seek bar in a page in my project. But whenever I use this code, I can't change brightness of phone via notification bar. It gets locked. How can I access both of them?

              val backLightValue = (it.view.progress / 100f)
              settingLawTextData.seekBarProgress = backLightValue
              val layoutParams = activity?.window?.attributes
              layoutParams?.screenBrightness = backLightValue
              activity?.window?.attributes = layoutParams

I also used this code to revert the brightness level to the original one, but still the brightness control seekbar in the notification panel is locked.

override fun onClick(v: View?) {
    when (v?.id) {
        R.id.imgCancel -> {
            val brightness = android.provider.Settings.System.getInt(activity?.contentResolver, android.provider.Settings.System.SCREEN_BRIGHTNESS)
            val layoutParams: WindowManager.LayoutParams = requireActivity().window.attributes
            layoutParams.screenBrightness = brightness / 100.0f
            activity?.window?.attributes = layoutParams

            findNavController().navigateUp()
        }
    }
}

Solution

  • Use this whenever you want to let the system gain power again to set the brighness:

            val layoutParams = requireActivity().window?.attributes
            layoutParams?.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
            requireActivity().window?.attributes = layoutParams