OnProgressChanged () SeekBar is called twice
I created a Custom SeekBarPreference
so that it displays the time range to be selected (say 0.2
sec to 2.2
sec). My problem is onProgressChanged () is being called twice - once with progress = 0
and the second time with progress = "actual value"
. I store the value in SharedPreference
so that I can later use it in my application.
Here is my code snippet -
@Override
protected View onCreateDialogView() {
LayoutInflater inflator = ((PreferenceActivity) mContext)
.getLayoutInflater();
View view = inflator.inflate(R.layout.seek_bar, null);
mCurrentValue = PreferenceManager.getDefaultSharedPreferences(mContext)
.getFloat(this.getKey(), mDefaultValue);
......
SeekBar mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
mSeekBar.setOnSeekBarChangeListener(this);
mSeekBar.setMax(20);
mSeekBar.setProgress((int) ((mCurrentValue - mMinValue)
/ (mMaxValue - mMinValue) * 20)); // 20 = progress bar max value.
return view;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (progress != 0) {
mCurrentValue = (float) ((float) progress / 20
* (mMaxValue - mMinValue) + mMinValue);
}
mValueText.setText(Float.toString(mCurrentValue));
/* mValueText is a TextBox where I display my Current Value */
}
Here I have checked the progress value so that it is not equal to 0
and therefore it avoids the first call. However, a side effect of this is that I cannot install anything for 0
.
Why can this be done?
source to share
The callback method onProgressChanged()
has a parameter boolean
fromUser
.
Using this parameter , we can determine if the changes come from the Framework or are made by the user .
So an implementation like -
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) { // If the changes are from the User only then accept the
// changes
/* Instead of -> if (progress != 0) { */
mCurrentValue = (float) ((float) progress / 20
* (mMaxValue - mMinValue) + mMinValue);
mCurrentValue *= 10;
mCurrentValue = Math.round(mCurrentValue);
mCurrentValue /= 10;
}
mValueText.setText(Float.toString(mCurrentValue));
}
sets the value TextView
only when it is changed by the user.
source to share
I have another problem today (Custom SeekBar and PreferenceFragment). I don't know why, but the code below only works twice: on start, touch and stop:
mTextViewValue = (TextView) view.findViewById(R.id.seekbar_value);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(@NonNull SeekBar seekBar, int progress, boolean fromUser) {
mTextViewValue.setText(String.valueOf(newValue));
}
Finally, I have a solution. It works:
final TextView valueTextView = (TextView) view.findViewById(R.id.seekbar_value);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(@NonNull SeekBar seekBar, int progress, boolean fromUser) {
valueTextView.setText(String.valueOf(newValue));
}
Maybe it helps someone.
source to share