Search code examples
androidnumberpicker

Android Numberpicker default value not coming


When the numberpicker is loaded, the default value is not appearing on the screen until touched.

Once touched, everything works fine.Any help appreciated.

Also if the formatter is removed, it works fine.

enter image description here

dialog.xml

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

<Button
    android:id="@+id/apply_button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/number_picker"
    android:text="@string/ok_string" />

Here is the activity code:

final NumberPicker np = (NumberPicker) d.findViewById(R.id.number_picker);
        np.setMaxValue(50);
        np.setMinValue(0);
        np.setWrapSelectorWheel(true);
        np.setOnValueChangedListener(this);
        np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

        np.setFormatter(new NumberPicker.Formatter() {
            @Override
            public String format(int i) {
                if (i == 25)
                    return "0";
                else
                    return String.valueOf(i - 25);
            }
        });
        np.setValue(25);

Thanks in advance


Solution

  • The issue seems to a bug in NumberPicker widget.

    And I have solved it using method 2 described here.