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 a XML file and 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 percetage.
Step 1
Now create an XML file and write this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#475d5d"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#354d5d"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="BrighnessControl"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:textStyle="bold">
</TextView>
<SeekBar
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:progress="0"
android:max="100"
android:layout_marginTop="20dp"
android:indeterminate="false"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:background="#354d5d"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="0"
android:textStyle="bold"
android:textSize="12dp"
android:textColor="#ffffff"
android:layout_marginLeft="25dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="12dp"
android:text="20"
android:textColor="#ffffff"
android:layout_marginLeft="30dp"/>
<TextView
android:layout_height="wrap_content"
android:textSize="12dp"
android:layout_width="wrap_content"
android:text="40"
android:textColor="#ffffff"
android:layout_marginLeft="32dp"/>
<TextView
android:layout_height="wrap_content"
android:textSize="12dp"
android:layout_width="wrap_content"
android:text="60"
android:textColor="#ffffff"
android:layout_marginLeft="36dp"/>
<TextView
android:textSize="12dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="80"
android:textColor="#ffffff"
android:layout_marginLeft="38dp"/>
<TextView
android:textSize="12dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="100"
android:textColor="#ffffff"
android:layout_marginLeft="42dp"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/txtPercentage"/>
</LinearLayout>
Step 2
Now create a Java file and write this.
First you need to use the SeekBar in the XML file and 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 percetage.
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 MainActivity extends Activity {//UI objects//
//Seek bar object
private SeekBar seekBar;
//Variable to store brightness value
private int brightness;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;
TextView txtPerc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate seekbar object
seekBar = (SeekBar) findViewById(R.id.seekBar);
txtPerc = (TextView) findViewById(R.id.txtPercentage);
//Get the content resolver
cResolver = getContentResolver();
//Get the current window
window = getWindow();
//Set the seekbar range between 0 and 255
//seek bar settings//
//sets the range between 0 and 255
seekBar.setMax(255);
//set the seek bar progress to 1
seekBar.setKeyProgressIncrement(1);
try
{
//Get the current system brightness
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
//Set the progress of the seek bar based on the system's brightness
seekBar.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
//Nothing handled here
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//Set the minimal brightness level
//if seek bar is 20 or any value below
if(progress<=20)
{
//Set the brightness to 20
brightness=20;
}
else //brightness is greater than 20
{
//Set brightness variable based on the progress bar
brightness = progress;
}
//Calculate the brightness percentage
float perc = (brightness /(float)255)*100;
//Set the brightness percentage
txtPerc.setText((int)perc +" %");
}
});
}}
Step 3
Add permission to the Android Menifest. xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidseekbarexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidseekbarexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 4
When you run your project:
Step 5
When you change the position of a bar:
Step 6
When you set it to 100%: