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. public class MainActivity extends Activity {//UI objects//
  13. //Seek bar object
  14. private SeekBar seekBar;
  15. //Variable to store brightness value
  16. private int brightness;
  17. //Content resolver used as a handle to the system's settings
  18. private ContentResolver cResolver;
  19. //Window object, that will store a reference to the current window
  20. private Window window;
  21. TextView txtPerc;
  22. /** Called when the activity is first created. */
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. //Instantiate seekbar object
  29. seekBar = (SeekBar) findViewById(R.id.seekBar);
  30. txtPerc = (TextView) findViewById(R.id.txtPercentage);
  31. //Get the content resolver
  32. cResolver = getContentResolver();
  33. //Get the current window
  34. window = getWindow();
  35. //Set the seekbar range between 0 and 255
  36. //seek bar settings//
  37. //sets the range between 0 and 255
  38. seekBar.setMax(255);
  39. //set the seek bar progress to 1
  40. seekBar.setKeyProgressIncrement(1);
  41. try
  42. {
  43. //Get the current system brightness
  44. brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
  45. }
  46. catch (SettingNotFoundException e)
  47. {
  48. //Throw an error case it couldn't be retrieved
  49. Log.e("Error", "Cannot access system brightness");
  50. e.printStackTrace();
  51. }
  52. //Set the progress of the seek bar based on the system's brightness
  53. seekBar.setProgress(brightness);
  54. //Register OnSeekBarChangeListener, so it can actually change values
  55. seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
  56. {
  57. public void onStopTrackingTouch(SeekBar seekBar)
  58. {
  59. //Set the system brightness using the brightness variable value
  60. System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
  61. //Get the current window attributes
  62. LayoutParams layoutpars = window.getAttributes();
  63. //Set the brightness of this window
  64. layoutpars.screenBrightness = brightness / (float)255;
  65. //Apply attribute changes to this window
  66. window.setAttributes(layoutpars);
  67. }
  68. public void onStartTrackingTouch(SeekBar seekBar)
  69. {
  70. //Nothing handled here
  71. }
  72. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
  73. {
  74. //Set the minimal brightness level
  75. //if seek bar is 20 or any value below
  76. if(progress<=20)
  77. {
  78. //Set the brightness to 20
  79. brightness=20;
  80. }
  81. else //brightness is greater than 20
  82. {
  83. //Set brightness variable based on the progress bar
  84. brightness = progress;
  85. }
  86. //Calculate the brightness percentage
  87. float perc = (brightness /(float)255)*100;
  88. //Set the brightness percentage
  89. txtPerc.setText((int)perc +" %");
  90. }
  91. });
  92. }}
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. <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
  7. <uses-sdk
  8. android:minSdkVersion="7"
  9. android:targetSdkVersion="16" />
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name="com.androidseekbarexample.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. </application>
  24. </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