I had the same problem and the accepted answer really helped me a lot. My situation is slightly different, although I am using DatePickerDialog . Here's how DatePicker worked DatePicker .
Declare variables first and then define them.
private DatePickerDialog.OnDateSetListener date; private DatePickerDialog mDatePickerDialog; private Calendar myCalendar; // Get the calendar instance myCalendar = Calendar.getInstance(); // Define the date set listener first. date = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // Do something with year, monthOfYear and dayOfMonth } }; // Now define the DatePickerDialog mDatePickerDialog = new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); mDatePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { DatePicker datePicker = mDatePickerDialog.getDatePicker(); // The following clear focus did the trick of saving the date while the date is put manually by the edit text. datePicker.clearFocus(); date.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth()); } });
Then inside the onClick button
mDatePickerDialog.show();
source share