Saturday, September 5, 2015

// //

Change screen brightness programmatically using seekbar

Hello, I'll be showing some helpfull tutorial stuff in android.
This time it well be on how to change system brightness..

Ok in your layout folder,

main.xml




    

    
    

 


and in your MainActivity.java
copy this code:
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class AndroidBrightnessActivity extends Activity {//UI objects//
 //Seek bar object
 private SeekBar brightbar;

 //Variable to store brightness value
 private int brightness;
 //Content resolver used as a handle to the system's settings
 private ContentResolver Conresolver;
 //Window object, that will store a reference to the current window
 private Window window;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Instantiate seekbar object
        brightbar = (SeekBar) findViewById(R.id.ChangeBright);
        
      
        //Get the content resolver
       Conresolver = getContentResolver();
        
        //Get the current window
        window = getWindow();
        
       
        brightbar.setMax(255);
       
        brightbar.setKeyProgressIncrement(1);
      
        try 
        {
         
         brightness = System.getInt(Conresolver, System.SCREEN_BRIGHTNESS);
  } 
        catch (SettingNotFoundException e) 
  {
         
   Log.e("Error", "Cannot access system brightness");
   e.printStackTrace();
  }
  
 
  brightbar.setProgress(brightness);

 
  brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
  {
   public void onStopTrackingTouch(SeekBar seekBar) 
   {
    System.putInt(Conresolver, System.SCREEN_BRIGHTNESS, brightness);
    
    LayoutParams layoutpars = window.getAttributes();
   
    layoutpars.screenBrightness = brightness / (float)255;
   
    window.setAttributes(layoutpars);
   }
   
   public void onStartTrackingTouch(SeekBar seekBar) 
   {
    
   }
   
   public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
   {
    
    if(progress<=20)
    {
     
     brightness=20;
    }
    else 
    {
     
     brightness = progress;
    }
   
    
   
   }
  }); 
    }}


Point to Note:

We have a variable called brightness because that is used to store
brightness value.

We have also used Content Resolver to access the system settings
(ex. brightness = System.getInt(Conresolver, System.SCREEN_BRIGHTNESS);
And also Window object, that will store a reference to the current window..

Now in your manifest:

Dont forget to add this permission:


And i think we're ready,
Note: Test it on the real device....
Thankyou...

You may also like:
Change system brightness using alertDialog
How to put seekbar in alertdialog android example