Search code examples
androidnumberpicker

Make NumberPicker string values repeatable


How to make the string values of my NumberPicker recurring when I move it in one direction? The following code makes the NumberPicker non-recurring. It stoppes when it goes from upside to downside and vise-versa.

Here is and xml snippet.

            <NumberPicker
            android:id="@+id/nu_plus_minus"
            android:layout_gravity="left|center_vertical"
            android:layout_width="@dimen/N_P_Width"
            android:layout_height="@dimen/N_P_Height"
            android:gravity="center"/>

Here is my code for this picker.

        NumberPicker nu_plus_minus = (NumberPicker)v.findViewById(R.id.nu_plus_minus);

    String[] values = new String[2];
    values[0] = getString(R.string.plus_for_NP);
    values[1] = getString(R.string.minus_for_NP);
    nu_plus_minus.setMaxValue(values.length - 1);
    nu_plus_minus.setMinValue(0);
    nu_plus_minus.setDisplayedValues(values);
    nu_plus_minus.setWrapSelectorWheel(true);
    nu_plus_minus.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

Solution

  • As per the docs for setWrapSelectorWheel:

    Note: If the number of items, i.e. the range ( getMaxValue() - getMinValue()) is less than the number of items shown on the selector wheel, the selector wheel will not wrap. Hence, in such a case calling this method is a NOP.

    You're only displaying two values, so I have a hunch that this is why it isn't wrapping the values around like you want it to.