screen brightness seekbar not working - android

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

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?

Android seekbar does not display the min and max values

I want to set the minimum and maximum values dynamically to the double hand seekbar. I got the min and maxvalues to the seekbar from JSON Response. But my seekbar does not display the min and max values. I have given the double hand seekbar is like this:
rangeSeekBar = new RangeSeekBar (seekbar_MinValue, seekbar_MaxValue, getActivity());
ViewGroup layout = (ViewGroup) view.findViewById(R.id.range_bar_LinearLayout);
layout.addView(rangeSeekBar);
rangeSeekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
// handle changed range values
Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
lsp_minvalue = minValue;
lsp_maxvalue = maxValue;
pt_lspMintv.setText(Integer.toString(lsp_minvalue));
pt_lspMaxtv.setText(Integer.toString(lsp_maxvalue));
}
});
// The problem is, at the time of setSelectedMinValue
int seekbar_MinValue = 0, seekbar_MaxValue = 0;
try {
seekbar_MinValue = Integer.parseInt(minSeekValue);
rangeSeekBar.setSelectedMinValue(seekbar_MinValue);
seekbar_MaxValue = Integer.parseInt(maxSeekValue);
rangeSeekBar.setSelectedMaxValue(seekbar_MaxValue);
}catch (NumberFormatException e){
e.printStackTrace();
Not use setOnRangeSeekBarChangeListener, You have to use this,
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(SeekbarActivity.this,"seek bar progress:"+progressChanged,
Toast.LENGTH_SHORT).show();
}
});

How to resize image by using seekbar?

I want to resize image by using seekbar. Seekbar is working but its not giving any good results. With the following code, the image is resized but just when you change the progress of seekbar for the first time. Please help
Seekbar listener
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
// TODO Auto-generated method stub
progresValue = progress;
resizeImage(image, progresValue);
}
});
Function to resize image
public void resizeImage(Bitmap bitmap, int progress) {
Bitmap bitmapSource = bitmap;
float bHeight = bitmapSource.getHeight();
float bWidth = bitmapSource.getWidth();
float factorH = progress / (float) bHeight;
float factorW = progress / (float) bWidth;
float factorToUse = (factorH > factorW) ? factorW : factorH;
Bitmap bm = Bitmap.createScaledBitmap(bitmapSource,
(int)(bWidth * factorToUse), (int)(bHeight * factorToUse),
false);
qImage.setImageBitmap(bm);
}
final SeekBar sk1 = (SeekBar) findViewById(R.id.seekBar1);
sk1.setMax(500);
sk1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(progress,progress);
img.setLayoutParams(params);
img.setMinimumWidth(progress);
img.setMinimumHeight(progress);
}
});

Android How to add intervals texts in a seekbar

I am using a seekbar, and I want to add certain marks in the seekbar with text to use it as a scale. The selected interval points should be highlighted.
The corresponding code is here:
seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setMax(max);
seekbar.setProgress(50);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
value.setText("SeekBar value is " + progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
To make the above seek bar you first have to add a measuring scale as a background drawable. Then you'll have to make textboxes for each interval on the measuring scale.
<SeekBar
....
android:progressDrawable="#drawable/progress"
android:thumb="#drawable/thumb"
....
/>
Then
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = ((int)Math.round(progress/interval))*interval;
seekBar.setProgress(progress);
if(progress==5) textBox5.setTextColor(Color.RED);
else if(progress==10) textBox10.setTextColor(Color.RED);
//......... for all the text boxes.
}
Actually, all The above solutions force to select the last progress point, which is not logic.
To make it mire accurete, Seek bar must detect which is the nearest dot and jump to it.
So below is the solution,
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progressStep = getMax() / dotsCount;
int lastDotProgress = Math.round(getProgress() / progressStep) * progressStep;
int nextDotProgress = lastDotProgress + progressStep;
int midBetweenDots = lastDotProgress + (progressStep / 2);
if (getProgress() > midBetweenDots)
seekBar.setProgress(nextDotProgress);
else
seekBar.setProgress(lastDotProgress);
}
You can use same behavior in onProgressChanged() if you want to prevent seeking between dots
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int s_size=5;
progress = ((int)Math.round(progress/s_size))*s_size;
seekBar.setProgress(progress);
textview.setText(progress + "");
}
what you want to settext get easily get from progress and
textview.settext() value

android.widget.SeekBar cannot be cast to android.widget.TextView

I am creating a brightness control dialog. However the problem I am having is that when I put textview then seekbar it causes this error:
android.widget.SeekBar cannot be cast to android.widget.TextView
For some reason it seems to work when the positioning on .xml follows seekbar then textview.
Here is some code:
LayoutInflater li_brightness = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View v_brightness = li_brightness.inflate(R.layout.options_dialog_brightness, (ViewGroup)findViewById(R.id.ll_brightness_layout));
bright_textView = (TextView)v_brightness.findViewById(R.id.brightness_text);
brightBar = (SeekBar)v_brightness.findViewById(R.id.seekbar_brightness);
confirm_brightness = (Button)v_brightness.findViewById(R.id.button_brightness);
cResolver = getContentResolver();
window = getWindow();
brightBar.setMax(100);
brightBar.setProgress(50);
brightBar.setKeyProgressIncrement(1);
bright_textView.setText("50".toString());
brightBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
float BackLightValue = (float)progress/100;
if(BackLightValue <= 0){
bright_textView.setText(String.valueOf(BackLightValue));
}
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
});
Try cleaning your project (Project > Clean from Eclipse or ant clean from the command line), and see if that clears up your problem. Sometimes the build system gets a bit out of sync with itself in terms of the R values, and this sort of error is one of the symptoms.

Categories

Resources