7

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:

<NumberPicker
    android:id="@+id/numberPicker1"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal" />

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

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

    np.setMinValue(1);
    np.setMaxValue(20);
    np.setWrapSelectorWheel(false);
    np.setDisplayedValues(nums);
    np.setValue(1);

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.

EDIT

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

Numberpicker screenshot

Any help would be appreciated.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Dogtag89
  • 73
  • 1
  • 1
  • 7
  • it looks like the `MatchPicker` inherits the `android:orientation` XML attribute from a parent class, so that might be the issue. try getting rid of that line too and see if it works. i'm trying to find a way to display the values horizontally online... – Alex Lockwood Feb 15 '12 at 13:36
  • Is there any way to get the background wheel image too on Android 4.0.x? It works like a charm on later versions. – Cagdas Altinkaya Jan 30 '13 at 14:26

2 Answers2

6

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.


EDIT:

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!

Community
  • 1
  • 1
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Hi thanks for your help, I have made the changes you advised me to do however the numberpicker is still not usable. – Dogtag89 Feb 15 '12 at 12:23
2

Change the height of the NumberPicker to 200dp. It's simply because it can't display the picker in the height you specified (48dp), because of this it messes up the rendering calculations.

Simon
  • 10,932
  • 50
  • 49