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?
Related
I have to set the volume of a video that is contained in a VideoView. I have to use a SeekBar(I already know how to work with SeekBars, I just would like to know how to set the volume of the video).
I also have to use another SeekBar as controll bar.
This is my code:
positionBar = (SeekBar) this.findViewById(R.id.seekBar2);
volumeBar = (SeekBar) this.findViewById(R.id.seekBar);
//getting the max volume and the current volume to set the volume bar
audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
currentVolume = audioManager.getStreamVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
volumeBar.setMax(maxVolume);
volumeBar.setProgress(currentVolume);
//putting the video to execute in the video view
videoPlayer.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video);
//volume bar listener
volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//********************************************************************
//position bar listener
positionBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//********************************************************************
//timer to update the position bar during the video execution
//and the volume
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
currentVolume = audioManager.getStreamVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
volumeBar.setProgress(currentVolume);
}
}, 0 , 500);
//********************************************************************
Check Below Code, To set the volume of the video programatically.
SeekBar volumeBar;
int volumeLevel;
AudioManager audioManager;
ImageView volumnBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
.....
.....
volumeBar = (SeekBar) findViewById(R.id.volumeBar);
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
volumeBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volumnBtn = (ImageView) findViewById(R.id.volumnBtn);
if (volumeLevel == 0) {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
} else {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
}
iv_volumn_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volumeBar.setProgress(volumeLevel);
if (volumeBar.getVisibility() == View.GONE) {
volumeBar.setVisibility(View.VISIBLE);
} else {
volumeBar.setVisibility(View.GONE);
}
}
});
volumeBar.setProgress(volumeLevel);
volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
volumeLevel = i;
if (volumeLevel == 0) {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
} else {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
}
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeLevel, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
volumeBar.setVisibility(View.GONE);
if (volumeLevel == 0) {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
} else {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
}
}
});
}
use SeekBar.getProgress() to get value of your seekbar,and then use the value to set volume,in onProgressChanged method.
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);
How can I change the contrast of screen in android through programming in android. I searched so many blog, it was not mentioned any where. Is it possible to control the contrast if possible please provide me the sample code.
Thanks in advance
see this link may be help to you....
Also see code
Settings.System.putInt(this.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 20);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.2f;// 100 / 100.0f;
getWindow().setAttributes(lp);
startActivity(new Intent(this,RefreshScreen.class));
public class MainActivity extends AppCompatActivity {
boolean isFrontOn;
ImageButton btnFront;
SeekBar brightnessBar;
float brightness;
ContentResolver cResolver;
RelativeLayout overlayLayout;
String permissions[] = {"android.permission.WRITE_SETTING"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFront = (ImageButton) findViewById(R.id.btnFront);
brightnessBar = (SeekBar) findViewById(R.id.brightnessBar);
cResolver = getContentResolver();
brightnessBar.setMax(255);
brightnessBar.setKeyProgressIncrement(1);
btnFront.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isFrontOn)
turnFrontOn();
else
turnFrontOff();
}
});
try {
brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
int screenBrightness = (int) brightness;
brightnessBar.setProgress(screenBrightness);
brightnessBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressVal = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress <= 20)
progressVal = 20;
else
progressVal = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (Build.VERSION.SDK_INT < 23 || checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_DENIED)
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, progressVal);
else requestPermissions(permissions, 0);
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if ( checkSelfPermission(permissions[0]) == PackageManager.PERMISSION_GRANTED) {
//ToDo your code here after checking permission
/*
i.e) Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, progressVal);
*/
}
}
}
try this in your code if your device API is greater than 22
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);
}
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...