How to dim screen brightness completely - android

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);

Related

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...

Change height and width of View with respect to seek bar changes in android

private OnSeekBarChangeListener _onSizeChangeListener = new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = progress+MIN_POINT_SIZE;
_view.getLayoutParams().height = progress;
_view.getLayoutParams().width = progress;
}
};
In the above piece of code,I want to change the width and height of _view as and when user seeks to some value from Seek bar control.But _view's attributes are not changing.Could some one look into it is there any wrong going?
Try this code in on progress changed,
LayoutParams newParams = new LayoutParams(progress,progress);
_view.setLayoutParams(newParams);
try to add:
_view.getRootView().requestLayout();
after you've update the LayoutParams dimensions.

Changing step values in seekbar?

I have a seekbar, while moving it I want to change values from 0 to 200. I have a TextView, where I display those values while moving the seekbar. But I don't want to have every value from that interval(0,1,2,3,4,5...), but to have 10, 20, 30...so on. So when I move the seekbar, I want to display 10,20,30,40....to 200. Can somebody give me a hint or an example how to do this?
Try below code
SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.incrementProgressBy(10);
seekBar.setMax(200);
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(tvRadius.getText().toString().trim());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 10;
progress = progress * 10;
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
setProgress(int) is used to set starting value of the seek bar
setMax(int) is used to set maximum value of seek bar
If you want to set boundaries of the seekbar then you can check the progressbar value in the onProgressChanged method. If the progress is less than or greater than the boundary then you can set the progress to the boundary you defined.
The easieset way I can think of, is simply defining:
SeekBar yourSeekBar = (SeekBar)findViewById(R.id.yourSeekBarId);
yourSeekbar.setMax(20);
Next, override those methods (the empty methods are also required, even if they are empty):
yourSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
if (progress >= 0 && progress <= sizeSeekBar.getMax()) {
String progressString = String.valueOf(progress * 10);
yourTextView.setText(progressString); // the TextView Reference
seekBar.setSecondaryProgress(progress);
}
}
}
});
The idea is to only define 20 values for the seekbar, but always multiply the value by 10 and display that value.
If you do not really need 200 values, then there is no point in using 200 values.
You can use the Slider provided by the Material Components Library using the android:stepSize attribute:
<com.google.android.material.slider.Slider
android:valueFrom="0"
android:valueTo="200"
android:stepSize="10"
..>
You should use the SeekBar.OnSeekBarChangeListener for tracking the progress change. Call mseek.setMax(200). Do a modulo operation on the current value to decide if the textview should be updated or not.
SeekBar.OnSeekBarChangeListener() {
void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress % 10 == 0) {
textview.setText(""+progress);
}
}
}
You can also achieve this by defining custom SeekBar class as below
public class CustomSeekBar extends AppCompatSeekBar {
int SEEKBAR_MULTIPLIER = 1;
public CustomSeekBar(Context context) {
super(context);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setSeekBarConfig( int SEEKBAR_MULTIPLIER, int SEEKBAR_MAX){
setMax(SEEKBAR_MAX);
this.SEEKBAR_MULTIPLIER = SEEKBAR_MULTIPLIER;
}
#Override
public int getProgress() {
return super.getProgress() * SEEKBAR_MULTIPLIER;
}
}
And can use by following manner
CustomSeekBar mCustomSeekBar = (CustomSeekBar) findViewById(R.id.customSeekBar);
mSeekBar.setSeekBarConfig(10,20);
In xml you should add CustomSeekBar instead of SeekBar
<*.CustomSeekBar
com.quemb.qmbform.view:setSeekBarConfig="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBar" />
So that you can reuse the custom seekBar in entire App with out repeating whole extra calculation.
for the minimum issue, you can set an offset on your progress. This way, the user can't go under your minimal value.
int offset = 10;
seekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.setMax(190); //with the offset, you need to adapt the max value
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(""+(seekBar.getProgress() + offset));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress += offset;
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
use Math.round function
fun round(n: Int): Int {
// Smaller multiple
val a = n / 10 * 10
// Larger multiple
val b = a + 10
// Return of closest of two
return if (n - a > b - n) b else a
}
One possible solution would be to set seekBar.setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10.
The SeekBar would then appear to move at least 20 at a time.

Android 2.2: Adjusting screen brightness

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...

Categories

Resources