Search code examples
androidnumberpicker

Invert Scroll Direction NumberPicker


How do i invert the scroll direction of a NumberPicker

enter image description here

I would like to have the 1 above the 0


Solution

  • A simple onCreate method ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.something);
        final TextView tv = (TextView) findViewById(R.id.tv);
        NumberPicker np = (NumberPicker) findViewById(R.id.np);
    
        final String[] values = {"1", "0"};
    
        np.setMinValue(0);
        np.setMaxValue(values.length - 1);
        np.setDisplayedValues(values);
        np.setWrapSelectorWheel(true);
        np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                String result = "Currently selected value:" + values[newVal];
                tv.setText(result);
            }
        });
    }
    

    ... and a simple something.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <NumberPicker
            android:id="@+id/np"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/np" />
    </RelativeLayout>