Hi,
Here I have written this code to change the brightness of the device in our application. I use the seekbar to change the brightness of the device. To handle the System Settings I use ContentResolver class and to handle current window reference, I used Window object.
First of all add this permission in manifest file.1
<uses-permission android:name="android.permission.WRITE_SETTINGS">
This is the xml file thats have TextView and Seekbar
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height=" match_parent "
android:orientation="vertical">
<textview android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Slide it.">
<seekbar android:id="@+id/seek_bar_brightness"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginleft="10p"
android:layout_marginright="10dp">
</seekbar>
</textview></linearlayout>
Add this to your Activity
private SeekBar seekBar;
//Variable to store brightness value
private int unitBrightNess;
private ContentResolver contentResolver;
//Window object, that will store a reference to the current window
private Window currentWindow;
TextView percentageBright;
seekBar = (SeekBar) findViewById(R.id. seek_bar_brightness);
percentageBright = (TextView) findViewById(R.id.txtPercentage);
contentResolver = getContentResolver();
currentWindow = getWindow();
seekBar.setMax(255);
seekBar.setKeyProgressIncrement(1);
try
{
unitBrightNess = System.getInt(contentResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException snfe)
{
snfe.printStackTrace();
}seekBar.setProgress(unitBrightNess);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar sBar)
{
LayoutParams lparms = currentWindow.getAttributes();
lparms.screenBrightness = currentWindow / (float)255;
currentWindow.setAttributes(lparms);
}
public void onStartTrackingTouch(SeekBar sBar)
{
//Nothing handled here
}
public void onProgressChanged(SeekBar sBar, int progressNumber, boolean fromUser)
{
if(progressNumber <=20)
{
unitBrightNess =20;
}
else
{
unitBrightNess = progressNumber;}
float percDecimal = (unitBrightNess /(float)255)*100;
percentageBright.setText((int) percDecimal +" %");
}
});
}}
0 Comment(s)