Android Question Changing screen brightness

William Hunter

Active Member
Licensed User
Longtime User
I have written a widget to change screen brightness. I would like some information re the values used for both WriteSetting, to change the system setting and Phone.SetScreenBrightness, to change current brightness.

As I understand it value has a range from 0 to 255, and that a very small value will turn the display completely off. I have read on the forum that values up to and including 15 will do this.

My Question: Can I view the value of 255 to represent 100% brightness, and that 50% of that value would represent 50% brightness? I’m looking for a way to scale that value, relative to actual brightness. Is there information available that would explain this? Any help is greatly appreciated.

Regards
 

William Hunter

Active Member
Licensed User
Longtime User
Round(Brightness Value / 255 x 100)

For example: Say you get the value 127 this will then represent 50%

Round(127 / 255 * 100) = 50

Thank you NJDude for your response. I need a little further clarification. It seems that we are dealing with two brightness scales. The first being (0 to 255), the second (0 to 100).

We also have:
1: WriteSetting, to change the system setting
and
2: Phone.SetScreenBrightness, to change current brightness

Question: In the example - Round(127 / 255 * 100) = 50, we have the start value of 127 and the rounded value of 50. Both of these values relate to 50% brightness. If brightness is set within a value of (0 to 50), as a product of the rounding formula, where does the 127 come into the mix? Are both of these values used to set the brightness? Say, one to change the system setting, and the other to change current brightness.

I’m confused as to why we are rounding the first value to produce the second. If only one value is required, why not just use the scale (0 to 100)? I expect there is a valid reason for the rounding process. I just don’t know what that reason is. Can you explain this to me?

Regards
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You are indeed confused :D.

The values to set the screen brightness is from 0 to 255, but, my understanding is that you want to know for example what percentage of brightness is 127, the answer is what I posted, that 50% is a conversion only you don't have to use the result to set the brightness, there are not 2 scales.

I did use round to well, get a round number or you might get something like 49.76% brightness.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
You are indeed confused :D.

Thank you for your response. Yes, I was definitely confused. I think I’ve been doing too much reading on the forum. The following code snippet is from this forum link:

http://www.b4x.com/android/forum/threads/phone-display-brightness.13021/#post-83857
B4X:
Dim p As Phone
If Value > 15 Then
    WriteSetting("screen_brightness", Value)
    p.SetScreenBrightness(Value/255)
EndIf

For WriteSetting the variable used is Value, whereas in p.SetScreenBrightness the variable used is (Value/255). That being the case, the code to set screen brightness level to 50% would be as below:
B4X:
Dim p As Phone
Dim Value As Float : Value = 127 
If Value > 15 Then
   WriteSetting("screen_brightness", Value)
   p.SetScreenBrightness(Value/255)
EndIf

I misread corwin42’s code example, and the inclusion of Rounding in your example got me into over-thinking-mode. Hence the source of my confusion. Thanks again for taking the time to set me straight. :cool:

Regards
 
Upvote 0
Top