2

At First I want to say that I'm using Android 5.1

I have problem with focused number in Number Picker widget. When I run my app and select fragment with this widget - focused number is blank. enter image description here

XML:

<NumberPicker
    android:id="@+id/NumberPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_gravity="center"
    android:descendantFocusability="blocksDescendants"
    />

Method from my fragment:

public void setNumberPickerParameters(){

    mNumPicker.setMinValue(1);
    mNumPicker.setMaxValue(18);
    mNumPicker.setValue(6);
    mNumPicker.setFormatter(new NumberPicker.Formatter() {
        @Override
        public String format(int value) {
            return String.valueOf(value * 10);
        }
    });
    Log.v(SETTINGS, "Picked number: " + mNumPicker.getValue() );

}
Johannes Jander
  • 4,974
  • 2
  • 31
  • 46

2 Answers2

2

I had the same problem, and I was thinking it was changing color to white when I tap the value, but that's not what it's causing it.

The real problem is the formatting. It's caused by input filter on EditText when setDisplayValues is not used. Unfortunately reflection is the solution:

try {
    Field f = NumberPicker.class.getDeclaredField("mInputText");
    f.setAccessible(true);
    EditText inputText = (EditText) f.get(numberPicker);
    inputText.setFilters(new InputFilter[0]);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
}
stamenicn
  • 21
  • 3
0

My experience on a different android version was not so good with NumberPicker.(I have not worked on the latest version)

There i extended the NP class with my own implementation.

A good example would be something like this :

Android NumberPicker: Set min, max, default from XML

Good Luck !!

Community
  • 1
  • 1
Kunal Khaire
  • 307
  • 2
  • 7