Android seekbar does not display the min and max values - android

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

Related

On Drage Event Seekbar Reset with 0

I've implemented a list of seekbars. According to my functionality, I want the sum of all seekbar not to exceed 100% for example if I drag first seekbar to 60% the next one will not exceed 40%.
I have made it work but whenever I drag the first one to some value (let's say 60 %) and then the second one to let's say 40% and then again when I try to drag the first one to decrease its value resets to 0. What is the problem with my code?
holder.shelfShareSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
remainingShelfShare = 100 - shelfShareSum;
holder.percentTextView.setText(String.valueOf(progress));
productShelfShareMap.get(((Products)list.get(position)).getProductID());
if(progress > remainingShelfShare){
holder.shelfShareSeekbar.setProgress(remainingShelfShare);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
list.get(position).setSetSlider(seekBar.getProgress());
shelfShareSum = 0;
productShelfShareMap.put(((Products)list.get(position)).getProductID(), Integer.parseInt(holder.percentTextView.getText().toString()));
for (HashMap.Entry<Integer, Integer> entry : productShelfShareMap.entrySet()){
shelfShareSum += entry.getValue();
}
}
});
Use SetMax function of seekbar to setvalue in SecondSeekbar.
int step = 1; // set it accordingly if you want data in 3.1,3.2 range
int max = 100;
int min; // if you want to set min criteria too.
shelfShareSeekbar.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)
{
// Ex :
// And finally set value of next seekbar based on firstone
secondseekbar.setMax((max - progress) / step);
}
}
);

Setting minimum position for Seekbar in Android?

Hey guys I'm trying to make a brightness bar in android and I want to set the position of the thumb to minimum like for example minimum value is 20, then the thumb should not get dragged before 20, that means 20 should be minimum, is this possible? I tried seekbar.setMax(20); but it's not what I want. Any help would be gladly appreciated! Thanks in advance!
seekbar.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) {
int MIN = 5;
if (progress < MIN) {
value.setText(" Time Interval (" + seektime + " sec)");
} else {
seektime = progress;
}
value.setText(" Time Interval (" + seektime + " sec)");
}
});

Android Seekbar with max progress != max value

I'd like to set a max progress value in a Seekbar that is not the same as the max value.
For example max would be 100 and max progress 50, so the progress slider could only be dragged to the middle. Is there a way to do that?
You can try the following
int seekBarMaxValue = 100;
int seekBarDraggableMaxValue = 50;
seekBar.setMax(seekBarMaxValue );
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangetListener() {
public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
seekBar.setProgress(progress > seekBarDraggableMaxValue ? seekBarDraggableMaxValue : progress);
}
});
Hope it helps.
It seems like you want three numbers only (ex. 0; 50; 100); just pretend each number is represents another number or multiply the progress by the common divisor:
Example
0 = 0 or (0 * 50)
1 = 50 or (1 * 50)
2 = 100 or (2 * 50)
int int_actual_number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
seek_sample.setMax(2);
seek_sample.setProgress(0);
seek_sample.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int_actual_number = progress * 50;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can use different common divisors and amount of numbers to test this method for your needs.

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

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

Categories

Resources