Date Picker Dialog
suggest changeThe dialog prompts the user to select a date with a DatePicker. It needs a context and the initial year, month, and day. The selected date comes back through DatePickerDialog.OnDateSetListener.
public void showDatePicker(Context context, int initialYear, int initialMonth, int initialDay) {
DatePickerDialog datePickerDialog = new DatePickerDialog(context,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datepicker, int year, int month, int day) {
// onDateSet can run twice on some versions. Ignore the call after the picker is gone.
if (datepicker.isShown()) {
// You now have the selected year, month, and day.
}
}
}, initialYear, initialMonth, initialDay);
datePickerDialog.show();
}
Month is an int from 0 for January through 11 for December.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents