23

I'm looking for something like the individual parts of the date picker dialog. A view that allows you to input integers (and only integers) that you can limit (between 1 and 10 for example), where you can use the keyboard or the arrows in the view itself. Does it exists?

It is for a dialog. A ready-made dialog to request an integer would also help.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

5 Answers5

23

The NumberPicker widget is probably what you want. Unfortunately it's located in com.android.internal.Widget.NumberPicker which we cannot get to through normal means.

There are two ways to use it:

  1. Copy the code from android source
  2. Use reflection to access the widget

Here's the xml for using it in a layout:

<com.android.internal.widget.NumberPicker
    android:id="@+id/picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Here's the reflection to set the NumberPicker settings (I have not tested this):

Object o = findViewById(R.id.picker);
Class c = o.getClass();
try 
{
    Method m = c.getMethod("setRange", int.class, int.class);
    m.invoke(o, 0, 9);
} 
catch (Exception e) 
{
    Log.e("", e.getMessage());
}

Since it's an internal widget and not in the SDK, future compatibility could be broken if you use reflection. It would be safest to roll your own from the source.

The original source for this information is shared in this Google Group.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
Will
  • 19,789
  • 10
  • 43
  • 45
7

The NumberPicker internal widget has been pulled from the Android source code and packaged for your use and you can find it here. Works great!

EDIT: Original link is down, you can find a copy of the widget here

Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
  • @AlanMoore so it seems.... Here's the source of it I used in my open source app: http://code.google.com/p/tippytipper/source/browse/trunk/Tippy%20Tipper/src/net/mandaria/tippytipper/widgets/NumberPicker.java – Bryan Denny Oct 16 '11 at 21:12
  • Second link appears broken as well – Forrest Bice Apr 08 '14 at 14:28
  • @ForrestBice I had re-arranged the source code into using an Android library project. New location of that file is here: https://code.google.com/p/tippytipper/source/browse/trunk/Tippy%20Tipper%20Library/src/net/mandaria/tippytipperlibrary/widgets/NumberPicker.java – Bryan Denny Apr 08 '14 at 14:38
4

As has been mentioned elsewhere, NumberPicker is now available in the Android SDK as of API 11 (Android 3.0):

http://developer.android.com/reference/android/widget/NumberPicker.html

For Android < 3.0, you can use the code here:
https://github.com/novak/numpicker-demo
https://github.com/mrn/numberpicker

Community
  • 1
  • 1
bmaupin
  • 14,427
  • 5
  • 89
  • 94
0

You can with EditText use android:inputType="number"

<EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:inputType="number" android:layout_width="wrap_content"></EditText>
Echilon
  • 10,064
  • 33
  • 131
  • 217
ismail uzunok
  • 163
  • 1
  • 1
  • 14
0

You can simply use an EditText and define inputType as number. E.g:

        <EditText
            android:id="@+id/etNumberInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:inputType="number" />

To limit the maximum value to, say 10, you can do it programmatically as:

        final EditText et = findViewById(R.id.etNumberInput);

        et.addTextChangedListener(new TextWatcher() {
          
            public void afterTextChanged(Editable s) {}
            
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {}
            
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                if (Integer.parseInt(et.getText().toString()) > 10) {
                    et.setError("***Your error here***");
                    // your logic here; to limit the user from inputting
                    // a value greater than specified limit
                }
            }
        });

This should meet your goal.

Yash Joshi
  • 557
  • 7
  • 25