0

I have a NumberPicker which should represent hours. How can I add the :00 after the number? Or is there another way? Here's the code:

hourPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    hourPicker.setMinValue(rightNow.get(Calendar.HOUR_OF_DAY) + 2);
    hourPicker.setMaxValue(23);
    hourPicker.setWrapSelectorWheel(false);

EDIT:

enter image description here

jlively
  • 735
  • 2
  • 9
  • 29

2 Answers2

1

According to this answer, there is no direct way to restrict or disable the subviews of a TimePicker. You can copy the code from the Android SDK and try to edit it in a way useful for you, but that won't be an usual behavior for such a widget.

The other way is to allow the user to pick whatever time he/she wants and show an error message after the time selection, stating the the selected hour is wrong.

EDIT: Please, refer to this question. Check out the answer with the most votes.

Community
  • 1
  • 1
Todor Kostov
  • 1,789
  • 1
  • 13
  • 20
  • Thank you for your answer, but I am not using TimePicker. I am using NumberPicker because I only needed to show hours. Now, everything works fine, but I get numbers like this "8,9,12,13,14". I would like to somehow format them so :00 is added to all of them. Check my edited answer for a screenshot. – jlively Jul 08 '16 at 10:32
  • So on the left picker you have a date and on the right one you have and hour, right? And you just want to add :00 after the right picker? – Todor Kostov Jul 08 '16 at 10:38
  • I want the add the :00 in the right picker, not after it. So basically, instead of 8 I want it to be 8:00. – jlively Jul 08 '16 at 10:46
  • Yes, this way I can hard code the numbers, but I can't make the min value to get the current hour and add +2. :( Maybe it has to do something with the .setFormatter but I can't seem to make it work. – jlively Jul 08 '16 at 11:04
  • Did you set **min/maxValue()**? – Todor Kostov Jul 08 '16 at 11:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116794/discussion-between-jlively-and-todor-kostov). – jlively Jul 08 '16 at 11:25
1

Maybe you can do this like so:

String[] displayedValues  = new String[15];//23-8=15, so lenght needed is 15

for(int i=0; i<16; i++){
    displayedValues[i] = String.valueOf(8+i)+":00";//gives 8:00 for i=0 till 23:00 for i=15
}

and than

hourPicker.setDisplayedValues(displayedValues);
Roman T.
  • 333
  • 3
  • 10