4

I have a EditText which when clicked creates a AlertDialog which contains a number picker. I want to give the user the option to scroll through the number picker and also to enter a value via keyboard. But i dont know how to set the keyboard type for NumberPicker currently the full keyboard displays when clicking on a value of the number picker.

I know EditText you can do editText.setInputType, but what about for NumberPicker?


Here is my dialog xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:holo="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >

<NumberPicker
    android:id="@+id/numberPicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>
</LinearLayout>

I dont want to set the input type of the edit text. I want to set the input type of the number picker. When the edit text is clicked a dialog is created which holds a number picker. Its that number picker that i want to set the input type on.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Jarrod
  • 71
  • 3
  • android:inputType="number". set this property for editext. I din't understand clearly the numberpicker part of the question – Raghunandan May 28 '13 at 13:35
  • As long as you implement the `` in your `AlertDialog` xml layout, then it will automatically allow for all of the features that you are looking for. If you are still having trouble then you should post your `AlertDialog` xml. – TronicZomB May 28 '13 at 13:36
  • I have posted my dialog xml – Jarrod May 28 '13 at 23:04
  • This is the functionality now by default. What theme are you using? – Droid Chris Apr 09 '15 at 23:09

2 Answers2

2

By default NumberPicker now uses inputType="number", unless you set displayedValues, which reverts the keyboard to full mode since displayed values is a String[].

In some use cases you may want to override this behaviour, e.g. if your values are non-consecutive numbers (0, 5, 10, etc.) or floats (1.0, 1.5, etc.). Here's a solution based on this question:

// Java
private void setInput(ViewGroup vg) {
    for(int i = 0; i < vg.getChildCount(); i++) {
        View child = vg.getChildAt(i);
        if(child instanceof ViewGroup) {
            // Recurse
            setInput((ViewGroup) child);
        } else if(child instanceof EditText) {
            // Force InputType
            ((EditText) child).setInputType(InputType.TYPE_CLASS_NUMBER);
        }
    }
}

...or...

// Kotlin
private fun setInput(vg: ViewGroup) {
    (0..vg.childCount).map { vg.getChildAt(it) }.forEach {
        when (it) {
            is ViewGroup -> setInput(it) // recurse
            is EditText -> it.inputType = InputType.TYPE_CLASS_NUMBER
        }
    }
}
charles-allen
  • 3,891
  • 2
  • 23
  • 35
  • I tried to set the input type this way, but this didn't work for me. Any guess why could it be? – P. Vera May 30 '19 at 11:02
  • Not any good guesses, other than perhaps Google changing their underlying implementation. Did you call `setInput(myNumberPicker)`? If you follow in the debugger does it find the EditText? – charles-allen May 31 '19 at 05:17
  • Yes I called the function that way, but I didn't get it to work. I finally managed to get rid of the `displayedValues` so my input type remains number. Thank you for your help anyway! – P. Vera May 31 '19 at 09:30
  • 1
    Works fine. But there is a little mistake in the code: `instanceOf` should be `instanceof` (2 times). – dexteritas Sep 06 '21 at 14:45
  • @dexteritas - Thanks; fixed it! I suspect I was using the Kotlin code in my project. – charles-allen Sep 07 '21 at 07:43
-3

just write in xml file:-

inputType="number"
Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61