Search code examples
androidscreen-brightness

how to incrase or decrease the brighness of the screen on button click


in my android app i want a single button in which on 1st click brightness will be 10 , on 2nd click brightness will be 50 , on third click brightness will be 255 (full ) as the user click next it will back to 10 again how to code this i have tired this solution but only able to increase

 private void getBrightness() {
    try 
        {   int curBrightnessValue = 0 ;
            curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
            if(curBrightnessValue >= 250  ) 
            {

            int SysBackLightValue = curBrightnessValue-30;


                   android.provider.Settings.System.putInt(getContentResolver(),
                   android.provider.Settings.System.SCREEN_BRIGHTNESS,
                   SysBackLightValue);
                   curBrightnessValue=SysBackLightValue;
            }
            else if(curBrightnessValue <250)

            {

                 int SysBackLightValue = curBrightnessValue+30;


                android.provider.Settings.System.putInt(getContentResolver(),
                   android.provider.Settings.System.SCREEN_BRIGHTNESS,
                   SysBackLightValue);
                   curBrightnessValue=SysBackLightValue;



            }

please help me to resolve this issue


Solution

  • Your code doesn't even do what you said you got in your question(you only try to increase and decrease by 30 without even applying it), you are getting the Brightness the right way, and all you have to do is to handle the new value in your Button Click Listener:

        private int getBrightness() {
            int curBrightnessValue = 0 ;
             try 
                {   
                  curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
                }
            catch(Exception ex){
                  curBrightnessValue = 0
                }
            return curBrightnessValue;
        }
    
        ok.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                int current_brightness = getBrightness();
                int new_brightness;
                if(current_brightness > 250)
                     new_brightness = 10;
                else if(current_brightness >= 50)
                     new_brightness = 255;
                else if(current_brightness >= 10)
                     new_brightness = 50;
                else
                     new_brightness = 150; // let's say this will be the default value
               // and to set the brightness
               set_system_brightness(new_brightness);
             }
        });
    
        private void set_system_brightness(int new_brightness){
            Settings.System
            .putInt(getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS,
                    new_brightness); // Set the system
                                // brightness
            android.view.WindowManager.LayoutParams w_lp = getWindow()
                    .getAttributes(); // Get the current window
                                        // attributes
            w_lp.screenBrightness = new_brightness / (float) 255; // Set
                                                            // the
                                                            // brightness
                                                            // of
                                                            // this
                                                            // window
            getWindow().setAttributes(w_lp); // Apply attribute
                                                // changes to
                                                // this window
        }