Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent a crash in the slider widget #1660

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ private void initialize(Context aContext) {

mSlider = findViewById(R.id.settings_slider);
mSlider.setStepSize(mStepSize);
mSlider.setValue(mInitialValue);
mSlider.setValueFrom(mValueFrom);
mSlider.setValueTo(mValueTo);
mSlider.setValue(adjustValueToSliderRange(mInitialValue));
mSlider.clearOnChangeListeners();
mSlider.addOnChangeListener(mInternalOnChangeListener);

Expand All @@ -81,9 +81,15 @@ public void onValueChange(@NonNull Slider slider, float value, boolean fromUser)
}
};

private float adjustValueToSliderRange(float value) {
// Prevent an exception if the value does not respect the step size, and min and max values.
float roundedValue = Math.round((value - mValueFrom) / mStepSize) * mStepSize + mValueFrom;
return Math.max(mValueFrom, Math.min(mValueTo, roundedValue));
}

public void setValue(float value, boolean doApply) {
mSlider.clearOnChangeListeners();
mSlider.setValue(value);
mSlider.setValue(adjustValueToSliderRange(value));
mSlider.addOnChangeListener(mInternalOnChangeListener);

if (mSliderListener != null && doApply) {
Expand Down
Loading