0

I have a numberpicker:

  AlertDialog alertDialog = builder.create();
  alertDialog.setTitle("Quantidade");
  NumberPicker NP = (NumberPicker)view.findViewById(R.id.npicker);
  NP.setMaxValue(1000);
  NP.setMinValue(1);
  alertDialog.setButton(...);
  alertDialog.show();

This works fine on Android 4.0.x, but on Android 2.3.x I get

java.lang.NoSuchMethodError: android.widget.NumberPicker.setMaxValue

If I remove NP.setMaxValue(1000) and NP.setMinValue(1), it works but the limits are set as 0, is there any way to set the number picker limits on Android 2.3.x?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Joaolvcm
  • 1,983
  • 3
  • 20
  • 24

2 Answers2

2

As per the documentation, NumberPicker (and all its methods) are available only from API Level 11 (SDK 3.0). So if you want compatibility with 2.3.x, you need to have your own implementation. Luckily there is one already available here

Edit

The Internet has been not so gentle on the link above and it is no longer available. Even the alternatives suggested in the comments are also gone. You are better off writing your own.

Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • i already knew that, but i saw that NumberPicker was showing up on android 2.3.x so i thought that there is any way to change the limits. he show up and works fine without any error but he only goes to 0 – Joaolvcm Apr 27 '12 at 14:36
  • 1
    As you can see from the website that the link points to, the component was available before, but was not part of the public API. So it is risky to use it even if you can compile with it. – Rajesh Apr 27 '12 at 14:39
  • Hello ! Why not the Google support Library support these errors ? – narancs Sep 18 '12 at 08:08
  • Good question, you should be asking it to Google. You can file a bug at https://code.google.com/p/android/issues/list – Rajesh Sep 18 '12 at 08:22
  • 1
    You can check these: http://quietlycoding.com/?p=5 and https://github.com/mrn/numberpicker – Rajesh Feb 01 '13 at 11:04
  • the link is still broken – Rishabh Agrawal Oct 08 '15 at 13:23
0

If you view the source code for the AOSP Messaging app on Github under the branch gingerbread-release, there is a NumPicker, written by Romain Guy that should allow you provide a NumberPicker to at least gingerbread.

If you copy the source code into your project, I would recommend dynamically choosing between that custom NumberPicker instance and the frameworks instance based on SDK version in order to use all of the new features and design based on the current API level.

Here is a link to the source code: https://github.com/android/platform_packages_apps_mms/blob/gingerbread-release/src/com/android/mms/ui/NumberPicker.java

I would recommend taking a peek around the project to get a fill of how to use it.

cincy_anddeveloper
  • 1,140
  • 1
  • 9
  • 19