Android Question SetScreenBrightness not working

Jose Miranda

Member
Licensed User
Longtime User
I am trying to change the brightness using the volume buttons. I was able to get and set the brightness (as in I'm not getting an error...) but its not working. If I Log the brightness value after changing it the value is unchanged...

This is my code:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
      Log("Brightness: " & p.GetSettings("screen_brightness"))

    If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
        p.SetScreenBrightness(Min((p.GetSettings("screen_brightness")+5),255)/255)
        Return True
      Else If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN Then
        p.SetScreenBrightness(Max((p.GetSettings("screen_brightness")-5),20)/255)
        Return True
      End If
End Sub

My phone (Note 2) returns brightness values from 20 to 255. Since the SetScreenBrightness function accepts a float from 0 to 1 I'm changing it to float by dividing by 255 (the max value). Im also limiting the values to a minimum of 20 and maximum of 255... Everything seems to be working since it is going into each of the if constructs... but still the brightness does not change!

What am I doing wrong?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
p.GetSettings("screen_brightness") returns the user settings. This value doesn't change when you set the brightness programmatically.

The following code is better:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   Dim r As Reflector
   r.Target = r.GetActivity
   r.Target = r.RunMethod("getWindow")
   r.Target = r.RunMethod("getAttributes")
  Dim current As Float = r.GetField("screenBrightness")
   Log(current)
  If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
     current = current + 0.1
  Else If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN Then
     current = current - 0.1
   Else
     Return False
   End If
   p.SetScreenBrightness(Min(1, Max(0, current)))
   Return True
End Sub
However if the screen brightness is set to automatic then the first value will be -1. You can explicitly set the brightness to 0.7 when your app starts.
 
Upvote 0

Jose Miranda

Member
Licensed User
Longtime User
p.GetSettings("screen_brightness") returns the user settings. This value doesn't change when you set the brightness programmatically.

The following code is better:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   Dim r As Reflector
   r.Target = r.GetActivity
   r.Target = r.RunMethod("getWindow")
   r.Target = r.RunMethod("getAttributes")
  Dim current As Float = r.GetField("screenBrightness")
   Log(current)
  If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
     current = current + 0.1
  Else If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN Then
     current = current - 0.1
   Else
     Return False
   End If
   p.SetScreenBrightness(Min(1, Max(0, current)))
   Return True
End Sub
However if the screen brightness is set to automatic then the first value will be -1. You can explicitly set the brightness to 0.7 when your app starts.

Your code worked flawlesly except for the use of r as a variable. For some reason it was already dimmed as a float... not in my code though...

Thanks!
 
Upvote 0

Jose Miranda

Member
Licensed User
Longtime User
I had to make some changes to your code in order to prevent "current" from getting out of the 0-1 bounds. Also added an extra check for the volume keys when the "current" value was at the upper and lower limit in order to return True (prevent sound volume from changing).

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    Dim ref As Reflector
      ref.Target = ref.GetActivity
      ref.Target = ref.RunMethod("getWindow")
      ref.Target = ref.RunMethod("getAttributes")
      Dim current As Float = ref.GetField("screenBrightness")

    If KeyCode = KeyCodes.KEYCODE_VOLUME_UP AND Round2(current,1) < 1 Then
        current = current + 0.1
      Else If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN AND Round2(current,1) > 0.1 Then
        current = current - 0.1
    Else If KeyCode <> KeyCodes.KEYCODE_VOLUME_UP AND KeyCode <> KeyCodes.KEYCODE_VOLUME_DOWN Then
        Return False
      End If
    Log(current)
    Log(Round2(current,1))

    p.SetScreenBrightness(Min(1, Max(0.01, current)))
      Return True
End Sub
 
Last edited:
Upvote 0

Jose Miranda

Member
Licensed User
Longtime User
By the way, I had to add the round2 functions since when I add or subtract 0.1, the resulting number is not exact (floating number precision).
 
Upvote 0

Adrian Jansen

Member
Licensed User
Longtime User
Came across this while trying to control the brightness, and after a lot of playing around discovered that the user settings ( in the phone toolbox ) have to be set to something other than -1 ( automatic ) before you can programatically change the brightness.

Just thought this may help someone else with this.
 
Upvote 0
Top