public DatePickerDialog (Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) was added in API level 1
Parameters:
context - The context the dialog is to run in.
callBack - How the parent is notified that the date is set.
year - The initial year of the dialog.
monthOfYear - The initial month of the dialog.
dayOfMonth - The initial day of the dialog.
1. In the MainActivity.java file, Declare a DatePickerDialog object named myDatePickerDialog in a private method with a return value, with the method named showDatePicker and and return value type of DatePickerDialog.
2. Also, add the below import to the imports section.
import android.widget.DatePicker;
3. Add the below code to the showDatePicker method. This will set the DatePickerDialog and initialize the year, month and day to 1999, 9 and 24 respectively. Adding a toast will display the year, month and day the user selected in the DatePickerDialog. This below would be in a class named MyClassName. You would change this to the class it is currently located within. The full class name is included because it’s inside a nested statement, therefore fully qualified names are required.
datePicker = new DatePickerDialog(MyClassName.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// the onDateSet method will get the year, month and day AFTER the user clicks the set button on the DatePickerDialog
//To test, add a toast, using a
//context of: getBaseContext()
//and a message of: ""Year: " + year + " Month: " + monthOfYear + " Day: " + dayOfMonth
//context of: getBaseContext()
//and a message of: ""Year: " + year + " Month: " + monthOfYear + " Day: " + dayOfMonth
}
} , 1999, 9, 24); // <- This will set the default date that will display on the DatePickerDialog when displayed
Next Recommended Article: Set DatePickerDialog results to a Calendar object
Resources:
http://developer.android.com/reference/android/app/DatePickerDialog.html#DatePickerDialog(android.content.Context, android.app.DatePickerDialog.OnDateSetListener, int, int, int)