So now you can introduce the 5 correction, like this...
final SeekBar sampleSizeSeekBar = (SeekBar)findViewById(R.id.sampleSizeSeekBar);
final int sampleSizeSeekBarCorrection = 5; //e.g., 95 <--> 100
int realValueFromPersistentStorage = appObj.getSampleSize(); //Get initial value from persistent storage, e.g., 100
sampleSizeSeekBar.setProgress(realValueFromPersistentStorage - sampleSizeSeekBarCorrection); //E.g., to convert real value of 100 to SeekBar value of 95.
sampleSizeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int val = sampleSizeSeekBar.getProgress();
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
val = progress + sampleSizeSeekBarCorrection; //e.g., to convert SeekBar value of 95 to real value of 100
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
try {
appObj.saveSampleSize(val); //Save real value to persistent storage, e.g., 100
appObj.makeToast("Sample size set to " + val);
}
catch(Exception e) {
Log.e(LOG_TAG, "Error saving sample size", e);
appObj.makeToast("Error saving sample size: " + e);
}
}
});
This solution will give your seekbar the correct min, max and scale on the UI, and the position of the control won't 'jump' to the right if the user slides it all the way to the left.
You can not set progress in float because seekbar.getProgress() always returns integer value. Following code will set textview value to 0.2 if seekbar value is 0. you can change minimumVal according to your requirement.
int minimumVal = 0;
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress >= minimumVal) {
seekBar.setProgress(progress);
textView.setText(progress);
} else {
seekBar.setProgress(minimumVal);
textView.setText("0.2");
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I finally found a way to set seekBarminimum value, just set set your setOnSeekBarChangeListener like this:
int minimumValue = 10; // your minimum value
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// you can set progress value on TextView as usual
txtProgress.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(seekBar.getProgress() < minimumValue)
seekBar.setProgress(minimumValue);
}
});