Deprecated: How to Display a Date 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 myDateButton.

2. Add a private class level integer constant named DATE_PICKER_DIALOG and set 0.

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

return showDatePicker();

4. Add a New method named updateDateButtonText 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. Set DatePickerDialog object with the DatePickerDialog initial values of myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH), and
myCalendar.get(Calendar.DAY_OF_MONTH)

7. In the onDateSet 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 myDateButton to the myString variable.

10. Compile and run!

Resources: