Deprecated: How to Display a Time Picker Dialog from a Button

Note: Some of these steps use deprecated items.



1. Create a Click Handler for a Button, using a method  named  registerButtonListenersAndSetDefaultText  and a private class level defined Button named myTimeButton.

2. Add a private class level integer constant named TIME_PICKER_DIALOG and set 1.

3. Add a Deprecated: onCreateDialog method to your class. Add the showDialog with a constant of TIME_PICKER_DIALOG to the onClick in the setOnClickListener in step 1. Add a case, named TIME_PICKER_DIALOG. In the case add the below line.

return showTimePicker();

4. Add a New method named updateTimeButtonText to your class, and call it from the registerButtonListenersAndSetDefaultText method, after the setOnClickListener.

5. In the onCreate method, Get Current Date/Time and Save to Calendar object with a private class level declared Calendar object named myCalendar.

6. Instantiate a TimePickerDialog object with the TimePickerDialog initial values of:

hour of: myCalendar.get(Calendar.HOUR_OF_DAY), 
minute of: myCalendar.get(Calendar.MINUTE), 
AM/PM of: true

7. In the onTimeSet method,
    - set the Calendar.YEAR to the year variable
    - set the Calendar.MONTH to the monthOfYear variable
    - set the Calendar.DAY_OF_MONTH to the dayOfMonth variable
    - then call the updateDateButtonText method

8. In the updateDateButtonText method, add a Format Calendar Date using SimpleDateFormat with a format of "yyyy-MM-dd".

9. Then set the Text for the Button named myTimeButton to the myString variable.

10. Compile and run!

Resources: