Android 2.2: Adjusting screen brightness - android

public void SetBright(float value)
{
Window mywindow = getWindow();
WindowManager.LayoutParams lp = mywindow.getAttributes();
lp.screenBrightness = value;
mywindow.setAttributes(lp);
}
I want to adjust the screen brightness. But nothing happens when i try using this method. Could it be because i use the KEEP_SCREEN_ON flag?

Make sure that "Auto Brightness" is not enabled prior to setting the screen brightness. You can do this manually in Settings>Display or using code if you are using Android 2.2 or above SDK.
Something like:
int brightnessMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 0.5F; // set 50% brightness
getWindow().setAttributes(layoutParams);
Ensure the value is between 0.0F and 1.0F. A value of -1.0F uses the default brightness stored in the preferences. Per the documentation "A value of less than 0, the default, means to use the preferred screen brightness. 0 to 1 adjusts the brightness from dark to full bright."

The value of screenBrightness between 0.0f and 1.0f, maybe your value is too big.
I use the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag and also can adjust the screen brightness

Adjust Brightness using a seekbar :
public class AndroidBrightnessActivity extends Activity {
private SeekBar brightbar;
private int brightness;
private ContentResolver cResolver;
private Window window;
TextView txtPerc;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
brightbar = (SeekBar) findViewById(R.id.brightbar);
txtPerc = (TextView) findViewById(R.id.txtPercentage);
cResolver = getContentResolver();
window = getWindow();
brightbar.setMax(255);
brightbar.setKeyProgressIncrement(1);
try
{
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
brightbar.setProgress(brightness);
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
System.putInt(cResolver,System.SCREEN_BRIGHTNESS,brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
//Nothing handled here
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if(progress<=20)
{
brightness=20;
}
else
{
brightness = progress;
}
float perc = (brightness /(float)255)*100;
txtPerc.setText((int)perc +" %");
}
});
}
}
Happy Coding...

Related

How to dim screen brightness completely

I want to fully dim my screen brightness programmatically using seekbar I am using this code to do the operation but it does not dim the screen brightness fully.I want to remove the screen brightness completely
code:
public class Night extends AppCompatActivity {
private SeekBar brightbar;
private int brightness;
private ContentResolver contentResolver;
private android.view.Window window;
TextView txtPerc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_night);
brightbar=(SeekBar)findViewById(R.id.brightbar);
txtPerc=(TextView)findViewById(R.id.txtPercentage);
contentResolver=getContentResolver();
window=getWindow();
brightbar.setMax(255);
brightbar.setKeyProgressIncrement(1);
try{
brightness= Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
}catch (Exception e) {
e.printStackTrace();
}
brightbar.setProgress(brightness);
brightbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress<=5)
{
brightness=5;
}else {
brightness=progress;
}
float perc=(brightness/(float)255)*100;
txtPerc.setText((int)perc + "%");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS,brightness);
WindowManager.LayoutParams layoutpars=window.getAttributes();
layoutpars.screenBrightness=brightness/(float)255;
window.setAttributes(layoutpars);
}
});
}
}
Can anyone suggest me how can I do that.
most "nightmode"/"protect your eyes" kind of apps uses a service with a View covering the whole screen.
they then set the transparency/alpha of this view according to how dark you want. the background color can be different hues or black.
take a look at this question which shows how to create a view from a service Starting a View from a Service?
hope it helps.
use bellow code it works for me
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);

changing brightness in android programmatically

when writing code for changing brightness programmatically in android studio, setContentView, getContentResolver, ChangeBright, findViewById and getWindow are in red. it says cannot resolve method. why is that so?
private SeekBar brightbar;
private int brightness;
private ContentResolver Conresolver;
private Window window;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab3);
brightbar = (SeekBar) findViewById(R.id.ChangeBright);
Conresolver = getContentResolver();
window = getWindow();
brightbar.setMax(255);
brightbar.setKeyProgressIncrement(1);
try
{
brightness = System.getInt(Conresolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
brightbar.setProgress(brightness);
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
System.putInt(Conresolver, System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if(progress<=20)
{
brightness=20;
}
else
{
brightness = progress;
}
Are you sure that your code extends Activity?

How to decrease brightness like screen filter

I have been trying to create a simple app like this but I failed.
What should I do based on this answer? The code I use:
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar fseekbar = (SeekBar) findViewById(R.id.fseekBar2);
fseekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
My relative Layout.xml
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<SeekBar
android:id="#+id/fseekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
The problem is that I cannot create a window manager and set to layoutParams the value of the brightness.
Try this...
MainActivity.java
public class MainActivity extends Activity {
private SeekBar brightbar;
//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.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
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
brightbar.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
brightbar.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 +" %");
}
});
}
}
main.xml
<?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:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Slide the bar to change the brightness:"
android:id="#+id/txtPercentage" />
<SeekBar
android:id="#+id/brightbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip" >
</SeekBar>
</LinearLayout>
Happy coding...

screen brightness seekbar not working

In my app I added a SeekBar which controls the screen brightness. It works some what fine .
I get the device's screen brightness in Oncreate() and set the progress of the SeekBar.
But when I change the seekbar progress , when I set it to the minimum(ie. to the zero position) the device gets stuck and I had to restart my device to get it working again.
What is wring with my code. Please help me
in oncreate() I have the following code,
try {
BackLightValue = android.provider.Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);
brightnessscrollbar.setProgress(BackLightValue);
System.out.println("Baclightvalue is "+BackLightValue);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekbarclicklistener
brightnessscrollbar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (int) ((float)arg1/100);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
int SysBackLightValue = (int)(BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
});
I came to know that Device gets stuck at the value zero
So I gave a condition on onProgresschanged as folows
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (int) ((float)arg1/100);
if(arg1>5){
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
}
And it works now, like a charm.
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
progress = progressValue;
if(progress <1 ) {
progress = 1;
}
android.provider.Settings.System.putInt(getActivity().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(getActivity().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.screenBrightness = progress / (float)255;
window.setAttributes(layoutParams);
}

Brightness in the application

I choose brightness in my app by this method
public void setBrightness(Context context, int progress) {
float BackLightValue = (float) progress/100;
WindowManager.LayoutParams layoutParams = ((Activity) context).getWindow().getAttributes();
if (BackLightValue == 0.0) {
BackLightValue = 0.01f;
}
layoutParams.screenBrightness = BackLightValue;
// layoutParams.screenBrightness = progress;
((Activity) context).getWindow().setAttributes(layoutParams);
}
this code works, but if I go to another activity brightness settings go back to previous.
How can I change brightness in all application? or I must to restore it on oncreate method on all activities?

Categories

Resources