Alexander Farber
Alexander Farber

Reputation: 22958

Custom NumberPickerPreference - how to set the NumberPicker value?

I have prepared a simple test case for my problem at GitHub.

I have extended a DialogPreference to a custom NumberPickerPreference - and it works well: displays a NumberPicker in a dialog and saves integer value when the dialog is closed.

My problem is that the saved integer value is never used to set the NumberPicker - when the dialog opened:

screenshot

In the above screenshot you can see (in the summary field at the bottom) that the saved integer value is 14 (and I see that value being passed to mPicker.setValue(mNumber) call in debugger) - but the NumberPicker is still scrolled to 1.

Here is the problematic NumberPickerPreference.java:

public class NumberPickerPreference extends DialogPreference implements OnValueChangeListener {
    private NumberPicker mPicker;
    private Integer mNumber = 0;
    private Integer mPickerValue = 0; // keeps picker value before dialog closes

    public NumberPickerPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setDialogLayoutResource(R.layout.preference_picker);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        mPicker = (NumberPicker) view.findViewById(R.id.picker);
        mPicker.setValue(mNumber);
        mPicker.setMinValue(1);
        mPicker.setMaxValue(100);
        mPicker.setOnValueChangedListener(this);
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        mPickerValue = newVal;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            setValue(mPickerValue);
        }
    }   

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mNumber) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        if (shouldPersist()) {
            persistInt(value);
        }

        if (value != mNumber) {
            mNumber = value;
            notifyChanged();
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, 0);
    }
}

And here is the layout file preference_picker.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="48dp"
    android:layout_marginTop="48dp"
    android:overScrollMode="ifContentScrolls" >

    <NumberPicker
        android:id="@+id/picker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</ScrollView>

Does anybody please know, what is missing here?

Initially I thought, that adding onGetDefaultValue method would help - like in my other question (Custom inline SeekBarPreference - how to set SeekBar progress on the 1st run?) - but it hasn't.

Upvotes: 2

Views: 1711

Answers (2)

Alexander Farber
Alexander Farber

Reputation: 22958

Okay I had to apply 2 fixes to my custom preference:

  1. mPicker.setValue() should be called after setMinValue() and setMaxValue() have been called.
  2. mPicker.clearFocus() should be called before reading its value - as workaround for situations when user edits the value in the TextEdit and then clicks OK button in the Dialog:

screenshot

Here is my fixed source code (layout file is not needed anymore) -

NumberPickerPreference.java:

public class NumberPickerPreference extends DialogPreference {
    private NumberPicker mPicker;
    private int mNumber = 0;

    public NumberPickerPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected View onCreateDialogView() {
        mPicker = new NumberPicker(getContext());
        mPicker.setMinValue(1);
        mPicker.setMaxValue(100);
        // should be called after setMinValue and setMaxValue
        mPicker.setValue(mNumber);
        return mPicker;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            // needed when user edits the text field and clicks OK
            mPicker.clearFocus();
            setValue(mPicker.getValue());
        }
    }   

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mNumber) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        if (shouldPersist()) {
            persistInt(value);
        }

        if (value != mNumber) {
            mNumber = value;
            notifyChanged();
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, 0);
    }
}

Upvotes: 4

Joozey
Joozey

Reputation: 238

After some testing I did not figure out why mPicker.setValue() in onBindDialogView() doesn't change the NumberPicker to the given value. However, to fix your problem, you can use setOnPreferenceClickListener and make a call to mPicker.setValue( mNumber );.

Truth be told, I'm not sure if this is a workaround or if this is how it is supposed to be.

Upvotes: 1

Related Questions