Implementing the DatePicker
or DatePickerDialog
in Android is easy. But when it comes to data storage, I have a problem with those classes:
If you use the spinners (+ or - button) to change the date, everything works fine. The event "Date changed" or "Date set" is called and you can get the values that the user entered.
But when the year is manually entered into the input field (via keyboard) and the user then clicks "Save" in the dialog, there won't be any event called and you won't get that manually entered value.
It only works when the user changes something with the sliders again after manually entering the year. Because when you use the sliders, the events are fired.
Is this normal behaviour? How can I achieve the desired behaviour, namely that an event is fired when the user enteres something manually and then clicks "Save"?
Thanks in advance!
If you look at the Date Picker Example, the DatePickerDialog.OnDateSetListener callback is received as soon as the user clicks the 'SET' button on the dialog.
Look at the dialog below
Even if you enter the date using the keyboard, the date itself is not accepted until you click the 'SET" button in the dialog and that is when the DatePickerDialog.OnDateSetListener callback is called.
I had the same problem and the accepted answer really helped me a lot. My situation is a little different though as I'm using
DatePickerDialog
. Here's how theDatePicker
properly worked finally.Declare the variables first and then define them.
Then inside an
onClick
of a buttonJust clear focus, and android will set the number from manual input.
eg:
When saving just like
onClick()
, adddatePicker.clearFocus()
;This must be working.