Java – How to use NumberPicker widget Android 4.0


I am trying to configure the NumberPicker widget using API 14. There are very little tutorials or instructions online that would help me set it up properly.

so far I have the following code implemented in main.xml:

    android:orientation="horizontal" />

and in the main activity in the onCreate() method:

NumberPicker np = (NumberPicker) findViewById(;
    String[] nums = new String[20];
    for(int i=0; i<nums.length; i++)
           nums[i] = Integer.toString(i);


When running my application in the emulator the numberpicker widget does not get displayed as expected. The button's are not left and right but up and down. And the values overlay each other instead of one number being displayed. Clicking an arrow crashes the application.


Made changes as described below in first answer however the numberpciker is not usable, please see screenshot:

Numberpicker screenshot

Any help would be appreciated.

Best Solution

Try commenting out the for loop and the line np.setDisplayedValues(nums);. The NumberPicker should display the values for you automatically.

You might also consider changing the width to android:layout_width="wrap_content" if you want the NumberPicker to display your values horizontally.


It appears that you cannot change NumberPicker (see this link). The issue is that you are trying to change the android:orientation attribute... but this is an inherited attribute from the LinearLayout class. You might be stuck with a vertically displaying NumberPicker unless you customize your own. Sorry!