Elliptica
Elliptica

Reputation: 4322

SeekBarPreference seekBarIncrement

I want to set seekBarIncrement in an xml (rather than programmatically). I have tried many variants of adding it to my xml, including in a style, as seekBarIncrement="100", asp:seekBarIncrement="100" etc. Nothing breaks/complains, but there is no increment either --- the seekbar values all differ only by 1, not 100, and if I put a log in code, it hasn't seen any increase there either.

How do I get seekBarIncrement to take effect? I'm using android.support.v7.preference.SeekBarPreference.

(I extended the class and do see its value, it just doesn't show anywhere or seem to affect anything)

For reference, the Support Library defines the following:

<declare-styleable name="SeekBarPreference">
    <attr format="integer" name="min"/>
    <attr name="android:max"/>
    <attr name="android:layout"/>
    <attr format="integer" name="seekBarIncrement"/>
    <attr format="boolean" name="adjustable"/>
    <attr format="boolean" name="showSeekBarValue"/>
</declare-styleable>

which one can see set in the SeekBarPreference class itself:

TypedArray a = context.obtainStyledAttributes(
            attrs, R.styleable.SeekBarPreference, defStyleAttr, defStyleRes);

    mMin = a.getInt(R.styleable.SeekBarPreference_min, 0);
    setMax(a.getInt(R.styleable.SeekBarPreference_android_max, 100));
    setSeekBarIncrement(a.getInt(R.styleable.SeekBarPreference_seekBarIncrement, 0));
    mAdjustable = a.getBoolean(R.styleable.SeekBarPreference_adjustable, true);
    mShowSeekBarValue = a.getBoolean(R.styleable.SeekBarPreference_showSeekBarValue, true);

Upvotes: 0

Views: 831

Answers (2)

Lasse Saranto
Lasse Saranto

Reputation: 11

I added some math to OnPreferenceChangeListener that uses the defined increment to round the value properly.

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    final String key = preference.getKey();
    if (key.equals(getString(R.string.key_scanning_delay))) {
        final SeekBarPreference sbp = (SeekBarPreference) preference;
        final int increment = sbp.getSeekBarIncrement();
        float value = (int) newValue;
        final int rounded = Math.round(value / increment);
        final int finalValue = rounded * increment;
        if (finalValue == value) return true;
        else sbp.setValue(finalValue);
        return false;
    }
    return true;
}

I'm using androidx.preference.SeekBarPreference, but there is probably little to no differences between these two libraries.

So, first check that it's the right preference. Then do the math. If the new calculated value is the same as the value the method was called with, return true so the value will be persisted. Most likely the values will differ on the first go. In that case, call the preference's setValue method again to update the view (and the value label if used). This time no math is needed (obviously) so it will return true and the value will be persisted. Finally return false so the original uncorrected value will be discarded.

Upvotes: 1

JensV
JensV

Reputation: 4544

In the documentation, the only xml attribute mentioned is android:thumb so what you're trying to do doesn't seem possible.

https://developer.android.com/reference/android/widget/SeekBar

I'd suggest going with either a programmatic approach or you could implement your own ViewGroup which accepts a parameter like seekBarIncrement and then passes it to the SeekBarPreference.

Upvotes: 0

Related Questions