Change Brightness of a Screen Using SeekBar in Android Studio

Introduction

 
This article explains how to use a seek bar to change the screen brightness in Android Studio.
 
First, you need to use a SeekBar in an XML file and create its id in a Java class file. Now get the content resolver object by calling getContentResolver() and the current window by calling the getWindow() methods. To get the brightness of a screen use the getint() method that returns the current brightness of a system. You will the brightness to the setProgress() method as an argument. Now set the seekbar on setOnSeekBarChangeListener() to change the brightness of a screen.
 
In the onProgress() method set the progress of the seek bar in percentage.
 
Step 1
 
Now create an XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="fill_parent"  
  4.    android:layout_height="fill_parent"  
  5.    android:background="#475d5d"  
  6.    android:orientation="vertical" >  
  7.    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  8.       android:layout_margin="5dp"  
  9.       android:layout_width="fill_parent"  
  10.       android:layout_height="wrap_content"  
  11.       android:background="#354d5d"  
  12.       android:orientation="vertical">  
  13.       <TextView  
  14.          android:layout_height="wrap_content"  
  15.          android:layout_width="wrap_content"  
  16.          android:text="BrighnessControl"  
  17.          android:layout_marginLeft="5dp"  
  18.          android:layout_marginTop="10dp"  
  19.          android:textColor="#ffffff"  
  20.          android:textStyle="bold">  
  21.       </TextView>  
  22.       <SeekBar  
  23.          android:layout_width="300dp"  
  24.          android:layout_height="wrap_content"  
  25.          android:id="@+id/seekBar"  
  26.          android:progress="0"  
  27.          android:max="100"  
  28.          android:layout_marginTop="20dp"  
  29.          android:indeterminate="false"  
  30.          />  
  31.       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  32.          android:layout_width="fill_parent"  
  33.          android:layout_height="25dp"  
  34.          android:background="#354d5d"  
  35.          android:orientation="horizontal"  
  36.          android:layout_marginTop="20dp">  
  37.          <TextView  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_width="wrap_content"  
  40.             android:text="0"  
  41.             android:textStyle="bold"  
  42.             android:textSize="12dp"  
  43.             android:textColor="#ffffff"  
  44.             android:layout_marginLeft="25dp"/>  
  45.          <TextView  
  46.             android:layout_height="wrap_content"  
  47.             android:layout_width="wrap_content"  
  48.             android:textSize="12dp"  
  49.             android:text="20"  
  50.             android:textColor="#ffffff"  
  51.             android:layout_marginLeft="30dp"/>  
  52.          <TextView  
  53.             android:layout_height="wrap_content"  
  54.             android:textSize="12dp"  
  55.             android:layout_width="wrap_content"  
  56.             android:text="40"  
  57.             android:textColor="#ffffff"  
  58.             android:layout_marginLeft="32dp"/>  
  59.          <TextView  
  60.             android:layout_height="wrap_content"  
  61.             android:textSize="12dp"  
  62.             android:layout_width="wrap_content"  
  63.             android:text="60"  
  64.             android:textColor="#ffffff"  
  65.             android:layout_marginLeft="36dp"/>  
  66.          <TextView  
  67.             android:textSize="12dp"  
  68.             android:layout_height="wrap_content"  
  69.             android:layout_width="wrap_content"  
  70.             android:text="80"  
  71.             android:textColor="#ffffff"  
  72.             android:layout_marginLeft="38dp"/>  
  73.          <TextView  
  74.             android:textSize="12dp"  
  75.             android:layout_height="wrap_content"  
  76.             android:layout_width="wrap_content"  
  77.             android:text="100"  
  78.             android:textColor="#ffffff"  
  79.             android:layout_marginLeft="42dp"/>  
  80.       </LinearLayout>  
  81.    </LinearLayout>  
  82.    <TextView  
  83.       android:layout_width="fill_parent"  
  84.       android:layout_height="wrap_content"  
  85.       android:text=""  
  86.       android:id="@+id/txtPercentage"/>  
  87. </LinearLayout> 
Step 2
 
Now create a Java file and write this.
 
First, you need to use the SeekBar in the XML file and create its id in the Java class file. Now get the content resolver object by calling getContentResolver() and the current window by calling the getWindow() methods. To get the brightness of a screen use the getint() method that returns the current brightness of a system. You will the brightness to the setProgress() method as an argument. Now set the seekbar on setOnSeekBarChangeListener() to change the brightness of a screen. 
 
In the onProgress() method set the progress of the seek bar in percentage.
  1. import android.app.Activity;  
  2. import android.content.ContentResolver;  
  3. import android.os.Bundle;  
  4. import android.provider.Settings.SettingNotFoundException;  
  5. import android.provider.Settings.System;  
  6. import android.util.Log;  
  7. import android.view.Window;  
  8. import android.view.WindowManager.LayoutParams;  
  9. import android.widget.SeekBar;  
  10. import android.widget.SeekBar.OnSeekBarChangeListener;  
  11. import android.widget.TextView;  
  12.    
  13. public class MainActivity extends Activity {//UI objects//  
  14.     //Seek bar object  
  15.     private SeekBar seekBar;  
  16.    
  17.     //Variable to store brightness value  
  18.     private int brightness;  
  19.     //Content resolver used as a handle to the system's settings  
  20.     private ContentResolver cResolver;  
  21.     //Window object, that will store a reference to the current window  
  22.     private Window window;  
  23.    
  24.     TextView txtPerc;  
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState)  
  28.     {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.    
  32.         //Instantiate seekbar object  
  33.         seekBar = (SeekBar) findViewById(R.id.seekBar);  
  34.    
  35.         txtPerc = (TextView) findViewById(R.id.txtPercentage);  
  36.    
  37.         //Get the content resolver  
  38.         cResolver =  getContentResolver();  
  39.    
  40.         //Get the current window  
  41.         window = getWindow();  
  42.    
  43.         //Set the seekbar range between 0 and 255  
  44.         //seek bar settings//  
  45.         //sets the range between 0 and 255  
  46.         seekBar.setMax(255);  
  47.         //set the seek bar progress to 1  
  48.         seekBar.setKeyProgressIncrement(1);  
  49.    
  50.         try  
  51.         {  
  52.             //Get the current system brightness  
  53.             brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);  
  54.         }  
  55.         catch (SettingNotFoundException e)  
  56.         {  
  57.             //Throw an error case it couldn't be retrieved  
  58.             Log.e("Error""Cannot access system brightness");  
  59.             e.printStackTrace();  
  60.         }  
  61.    
  62.         //Set the progress of the seek bar based on the system's brightness  
  63.         seekBar.setProgress(brightness);  
  64.    
  65.         //Register OnSeekBarChangeListener, so it can actually change values  
  66.         seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()  
  67.         {  
  68.             public void onStopTrackingTouch(SeekBar seekBar)  
  69.             {  
  70.                 //Set the system brightness using the brightness variable value  
  71.                 System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);  
  72.                 //Get the current window attributes  
  73.                 LayoutParams layoutpars = window.getAttributes();  
  74.                 //Set the brightness of this window  
  75.                 layoutpars.screenBrightness = brightness / (float)255;  
  76.                 //Apply attribute changes to this window  
  77.                 window.setAttributes(layoutpars);  
  78.             }  
  79.    
  80.             public void onStartTrackingTouch(SeekBar seekBar)  
  81.             {  
  82.                 //Nothing handled here  
  83.             }  
  84.    
  85.             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)  
  86.             {  
  87.                 //Set the minimal brightness level  
  88.                 //if seek bar is 20 or any value below  
  89.                 if(progress<=20)  
  90.                 {  
  91.                     //Set the brightness to 20  
  92.                     brightness=20;  
  93.                 }  
  94.                 else //brightness is greater than 20  
  95.                 {  
  96.                     //Set brightness variable based on the progress bar  
  97.                     brightness = progress;  
  98.                 }  
  99.                 //Calculate the brightness percentage  
  100.                 float perc = (brightness /(float)255)*100;  
  101.                 //Set the brightness percentage  
  102.                 txtPerc.setText((int)perc +" %");  
  103.             }  
  104.         });  
  105.     }} 
Step 3
 
Add permission to the Android Manifest.xml file.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.androidseekbarexample"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-permission android:name="android.permission.WRITE_SETTINGS"/>  
  8.     <uses-sdk  
  9.         android:minSdkVersion="7"  
  10.         android:targetSdkVersion="16" />  
  11.    
  12.     <application  
  13.         android:allowBackup="true"  
  14.         android:icon="@drawable/ic_launcher"  
  15.         android:label="@string/app_name"  
  16.         android:theme="@style/AppTheme" >  
  17.         <activity  
  18.             android:name="com.androidseekbarexample.MainActivity"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.    
  23.                 <category android:name="android.intent.category.LAUNCHER" />  
  24.             </intent-filter>  
  25.         </activity>  
  26.     </application>  
  27.    
  28. </manifest> 
Step 4
 
When you run your project:
 
1.jpg
 
Step 5
 
When you change the position of a bar:
 
2.jpg
 
Step 6
 
When you set it to 100%:
 
3.jpg