title: Custom NumberPickerPreference - how to set the NumberPicker value?
body: <p>I have prepared <a href="https://github.com/afarber/android-newbie/tree/q22/MyPrefs" rel="nofollow noreferrer">a simple test case</a> for my problem at GitHub.</p>
  
  <p>I have extended a <a href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/preference/DialogPreference.java" rel="nofollow noreferrer">DialogPreference</a> to a custom <a href="https://github.com/afarber/android-newbie/blob/q22/MyPrefs/src/de/afarber/myprefs/NumberPickerPreference.java" rel="nofollow noreferrer">NumberPickerPreference</a> - and it works well: displays a <code>NumberPicker</code> in a dialog and saves integer value when the dialog is closed.</p>
  
  <p>My problem is that the saved integer value is never used to set the <code>NumberPicker</code> - when the dialog opened:</p>
  
  <p><img src="https://i.stack.imgur.com/XnZeJ.png" alt="screenshot"></p>
  
  <p>In the above screenshot you can see (in the summary field at the bottom) that the saved integer value is <strong>14</strong> (and I see that value being passed to <code>mPicker.setValue(mNumber)</code> call in debugger) - but the <code>NumberPicker</code> is still scrolled to <strong>1</strong>.</p>
  
  <p>Here is the problematic <a href="https://github.com/afarber/android-newbie/blob/q22/MyPrefs/src/de/afarber/myprefs/NumberPickerPreference.java" rel="nofollow noreferrer">NumberPickerPreference.java</a>:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>And here is the layout file <a href="https://github.com/afarber/android-newbie/blob/q22/MyPrefs/res/layout/preference_picker.xml" rel="nofollow noreferrer">preference_picker.xml</a>:</p>
  
  <pre><code>&lt;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" &gt;
  
      &lt;NumberPicker
          android:id="@+id/picker"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" /&gt;
  
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>Does anybody please know, what is missing here?</p>
  
  <p>Initially I thought, that adding <code>onGetDefaultValue</code> method would help - like in my other question (<a href="https://stackoverflow.com/questions/30170384/custom-inline-seekbarpreference-how-to-set-seekbar-progress-on-the-1st-run">Custom inline SeekBarPreference - how to set SeekBar progress on the 1st run?</a>) - but it hasn't.</p>