Android NumberPicker widget – Part 1

I’m working on an Android application that requires a NumberPicker widget, the widget behind the DatePicker. Luckily, I’m not the only one and QuietlyCoding shows us how to clone and use the NumberPicker widget.

Then I started using the NumberPicker and I found out that not all is working as I would like to see. There are 2 things bugging me:

  • Entering numbers via the EditText (mText widget) do not stick. The mCurrent variable is not updated.
  • Variables are entered twice, entering a ‘2’ via the EditText widgets results in ’22’ being displayed.

I’m not sure yet what to do but I think I need to write my own NumberPicker or update the existing one, one that

  • looks like the real NumberPicker widget.
  • Behaves like the real NumberPicker (well… partly).
  • Accepts data from the EditText widget.
  • Does not use the ‘mDisplayedValues’. (I think this is implemented to support the DatePicker)
Advertisements
This entry was posted in Post Android, User Interfaces and tagged , . Bookmark the permalink.

6 Responses to Android NumberPicker widget – Part 1

  1. I added 2 changes to the NumberPicker to disable manual entry:
    1) Added the following lines to ‘OnCreate()’ in NumberPicker.java, just below mText.setRawInputType(InputType.TYPE_CLASS_NUMBER);:
    mText.setInputType(0); //See http://code.google.com/p/android/issues/detail?id=7115
    mText.setEnabled(false); // Disable the EditText widget.

    2) Updated the setEnabled() member. I replaced mText.setEnabled(enabled) with mText.setEnabled(false)

    Both changes make sure that the user cannot enter a value manually, he/she has to use the ‘+’ and ‘-‘ button. My problem with the NumberPicker is that the value is only stored when the user hits enter after manually entering a value. I want the entry to be used without hitting the enter key. Just try it for yourself, enter a value and press ‘+’ or ‘-‘.

  2. Fran Fitzpatrick says:

    I’m also running into the problem of the values not sticking when manually editing the EditText field. However, I WANT my users to have the option of doing this.

    Have you found out a solution?

    In addition the NumberPicker dialog seems to use the displayed values as long as it is set with setRange(int start, int end, String[] dispvalues) or whatever. Let me know if you have any questions.

    • ScottyB says:

      @Fran Fitzpatrick
      Old post I know – but maybe help for others.

      A quick easy win to the ‘edit text not sticking’ problem

      When you get the result – don’t call the ‘getCurrent()’ method.
      Interrogate the field directly
      i.e.

      EditText number = (EditText) dialog .findViewById(R.id.timepicker_input);

      int myNewNumber = Integer.parseInt(number.getText()
      .toString()));

      Hope that helps

  3. Junzi says:

    I have tried to add a OnEditorActionListener to the NumberClass, seems it solved the problem for me.

    – modify the NumberPicker.java let it extends OnEditorActionListener.
    – add **mText.setOnEditorActionListener(this);** to NumberPicker constructor
    – implement **onEditorAction**:

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    validateInput(v);
    return false;
    }

    Hope it can be helpful.

  4. CFranko says:

    This post is a extraordinarily gracious one. Thank for stakeing such great information out. Ill deff be driving by more often so i an take in whats informative!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s