Android Seekbar Minimum And Continuous Float Value Attributes
I'm looking for a way to implement a minimum value in my SeekBar and also have the option to increment decimal numbers. For example, currently my SeekBar's minimum is set to 0, but
Solution 1:
The values that can be used for the progress are limited to values between zero and some max value that we choose. We can however scale these values with arithmetic operations. Lets for example set the max value to 200 in the XML-file. Then change the java code in the following way.
@OverridepublicvoidonProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
sliderValueTW.setText(Float.toString((float)(progress-100)/10));
}
The result is that the slider appears to the user to be going from -10.0 to 10.0 with increments of 0.1 units.
Solution 2:
to set the starting progress you can do:
SeekBaryourSeekBar= (SeekBar) findViewById(R.id.seek_bar);
yourSeekBar.setProgress(20);
That should show your seekbar with your desired starting position (at 0.2) if the max is set to 100.
Solution 3:
I see you already have the first part which is how to do floating point values. Here's how I implement a minimum constraint: (ignoring the floating point value conversion)
int mySbValue;
SeekBarsb= (SeekBar) findViewById(R.id.seekbar);
sb.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener() {
@OverridepublicvoidonProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress < MIN) {
mySbValue = MIN;
} else {
mySbValue = progress;
}
}
...
Solution 4:
Copy this class and use custom Seek Bar :
publicclassFloatSeekBarextendsSeekBarimplementsSeekBar.OnSeekBarChangeListener {
private OnFloatSeekBarChangeListener floatListener;
privatefloatfloatMaxValue=100.f;
privatefloatfloatPrgress=0.f;
privatefloatminPrgress=0.f;
publicfloatgetMinPrgress() {
return minPrgress;
}
publicvoidsetMin(float minPrgress) {
this.minPrgress = minPrgress;
intmiddle= getMiddle(floatMaxValue, minPrgress);
super.setMax(middle);
}
privateintgetMiddle(float floatMaxValue, float minPrgress) {
floatv= floatMaxValue - minPrgress;
intround= Math.round(v * 10);
return round;
}
publicfloatgetFloatPrgress() {
return floatPrgress;
}
publicvoidsetFloatPrgress(float floatPrgress) {
this.floatPrgress = floatPrgress;
inti= Math.round((floatPrgress - minPrgress) * 10);
super.setProgress(i);
}
publicFloatSeekBar(Context context) {
super(context);
this.setOnSeekBarChangeListener(this);
}
publicFloatSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArraytypedArray= context.getTheme().obtainStyledAttributes(attrs, R.styleable.FloatSeekBar, 0, 0);
floatMaxValue = typedArray.getFloat(R.styleable.FloatSeekBar_maxValue, 100f);
minPrgress = typedArray.getFloat(R.styleable.FloatSeekBar_minValue, 0f);
floatPrgress = typedArray.getFloat(R.styleable.FloatSeekBar_floatProgress, 0f);
setFloatPrgress(floatPrgress);
this.setOnSeekBarChangeListener(this);
}
publicfloatgetMaxProgress() {
return floatMaxValue;
}
publicvoidsetMax(float value) {
floatMaxValue = value;
intmiddle= getMiddle(floatMaxValue, minPrgress);
super.setMax(middle);
}
publicvoidsetOnFloatSeekBarChangeListener(OnFloatSeekBarChangeListener floatListener) {
this.floatListener = floatListener;
intmiddle= getMiddle(floatMaxValue, minPrgress);
super.setMax(middle);
}
@OverridepublicvoidonProgressChanged(SeekBar seekBar, int i, boolean b) {
floatPrgress = minPrgress + i / 10.0f;
floatListener.onFloatSeekProgressChanged(seekBar, floatPrgress, b);
}
@OverridepublicvoidonStartTrackingTouch(SeekBar seekBar) {
floatListener.onStartTrackingTouch(seekBar);
}
@OverridepublicvoidonStopTrackingTouch(SeekBar seekBar) {
floatListener.onStopTrackingTouch(seekBar);
}
publicstaticinterfaceOnFloatSeekBarChangeListener {
publicvoidonFloatSeekProgressChanged(SeekBar seekBar, float i, boolean b);
publicvoidonStartTrackingTouch(SeekBar seekBar);
publicvoidonStopTrackingTouch(SeekBar seekBar);
}
@OverrideprotectedsynchronizedvoidonDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
Post a Comment for "Android Seekbar Minimum And Continuous Float Value Attributes"