Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NumberPicker with Formatter doesn't format on first rendering

I have a NumberPicker that has a formatter that formats the displayed numbers either when the NumberPicker spins or when a value is entered manually. This works fine, but when the NumberPicker is first shown and I initialize it with setValue(0) the 0 does not get formatted (it should display as "-" instead of 0). As soon as I spin the NumberPicker from that point on everything works.

How can I force the NumberPicker to format always - Both on first rendering and also when I enter a number manually with the keyboard?

This is my formatter

public class PickerFormatter implements Formatter {   private String mSingle;  private String mMultiple;   public PickerFormatter(String single, String multiple) {     mSingle = single;     mMultiple = multiple;  }   @Override  public String format(int num) {     if (num == 0) {         return "-";     }     if (num == 1) {         return num + " " + mSingle;     }     return num + " " + mMultiple;  }  } 

I add my formatter to the picker with setFormatter(), this is all I do to the picker.

    picker.setMaxValue(max);     picker.setMinValue(min);     picker.setFormatter(new PickerFormatter(single, multiple));     picker.setWrapSelectorWheel(wrap); 
like image 600
A. Steenbergen Avatar asked Jul 17 '13 19:07

A. Steenbergen


1 Answers

dgel's solution doesn't work for me: when I tap on the picker, formatting disappears again. This bug is caused by input filter set on EditText inside NumberPicker when setDisplayValues isn't used. So I came up with this workaround:

Field f = NumberPicker.class.getDeclaredField("mInputText"); f.setAccessible(true); EditText inputText = (EditText)f.get(mPicker); inputText.setFilters(new InputFilter[0]); 
like image 78
torvin Avatar answered Oct 16 '22 14:10

torvin