In this tutorial i show you,how to adjust screen brightness programatically in android.In this example we learn get current screen brightness state and changing screen brightness.
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Slide seekbar to change the brightness" />
<TextView
android:id="@+id/txtPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="0%"
android:textAppearance="?android:attr/textAppearanceLarge" />
<SeekBar
android:id="@+id/brightbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.Androidapplink.setscreenbrightnessapp;
import dev.Androidapplink.setscreenbrightnessapp.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
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 {
//Seek bar object
private SeekBar brightbar;
//Variable to store brightness value
private int brightness;
//handle to the system's settings
private ContentResolver cResolver;
//Window object store a reference to the current window
private Window window;
TextView txtPerc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate seekbar object
brightbar = (SeekBar) findViewById(R.id.brightbar);
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
brightbar.setMax(255);
//Set the seek bar progress to 1
brightbar.setKeyProgressIncrement(1);
try {
//Get the current system brightness state
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
float perc = (brightness /(float)255)*100;
txtPerc.setText((int)perc + "%");
} catch (Exception e) {
// TODO: handle exception
//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
brightbar.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//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);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Nothing handled here
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
//Set the minimal brightness level
if(progress<=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 + "%");
}
});
}
//IT WORK ONLY,IF SCREEN BRIGHTNESS IS NOT SETED IN "AUTO BRIGHTNESS MODE".
}
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Slide seekbar to change the brightness" />
<TextView
android:id="@+id/txtPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="0%"
android:textAppearance="?android:attr/textAppearanceLarge" />
<SeekBar
android:id="@+id/brightbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.Androidapplink.setscreenbrightnessapp;
import dev.Androidapplink.setscreenbrightnessapp.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
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 {
//Seek bar object
private SeekBar brightbar;
//Variable to store brightness value
private int brightness;
//handle to the system's settings
private ContentResolver cResolver;
//Window object store a reference to the current window
private Window window;
TextView txtPerc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate seekbar object
brightbar = (SeekBar) findViewById(R.id.brightbar);
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
brightbar.setMax(255);
//Set the seek bar progress to 1
brightbar.setKeyProgressIncrement(1);
try {
//Get the current system brightness state
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
float perc = (brightness /(float)255)*100;
txtPerc.setText((int)perc + "%");
} catch (Exception e) {
// TODO: handle exception
//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
brightbar.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//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);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Nothing handled here
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
//Set the minimal brightness level
if(progress<=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 + "%");
}
});
}
//IT WORK ONLY,IF SCREEN BRIGHTNESS IS NOT SETED IN "AUTO BRIGHTNESS MODE".
}
NOTE: Give permission in AndroidManifest.xml
It allow to change setting.
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Step 3: Run project and see Output:
Renaldi said: nice tutorial
ReplyDelete