DatePickerDialog OK button not getting the selected date

Compaq LE2202x picture Compaq LE2202x · Dec 19, 2013 · Viewed 13.2k times · Source

I have a simple DatePickerDialog that opens when an EditText is opened. After choosing a date and pressing OK, it should be displayed at the same EditText. It works fine if I only use the default dialog where it creates only one button - OK. I added a Cancel button, the problem is that it only gets the current date.

Here's my code:

    private void showDatePicker(String birthdayStr) {
        // TODO Auto-generated method stub
        final Calendar c = Calendar.getInstance();

        if (birthdayStr.equals("")) {
            yearStr = c.get(Calendar.YEAR);
            monthStr = c.get(Calendar.MONTH);
            dayStr = c.get(Calendar.DAY_OF_MONTH);
        }

        DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            // when dialog box is closed, below method will be called.
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {
                if (isOkayClicked) {
                    birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);
                    yearStr = selectedYear;
                    monthStr = selectedMonth;
                    dayStr = selectedDay;
                }
                isOkayClicked = false;
            }
        };
        DatePickerDialog datePickerDialog = new DatePickerDialog(
                RegistrationTwoActivity.this, datePickerListener, yearStr,
                monthStr, dayStr);

        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == DialogInterface.BUTTON_NEGATIVE) {
                            dialog.cancel();
                            isOkayClicked = false;
                        }
                    }
                });

        datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == DialogInterface.BUTTON_POSITIVE) {
                            isOkayClicked = true;
                            birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay);
                        }
                    }
                });
        datePickerDialog.setCancelable(false);
        datePickerDialog.show();
    }

If I delete the line birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); under OK or BUTTON_POSITIVE, it works fine. But on some devices, it doesn't set the selected date to the EditText since it was only called inside datePickerListener. So I decided adding the line birthday.setText(selectedYear + (selectedMonth + 1) + selectedDay); under OK or BUTTON_POSITIVE but the problem now is that it only gets the current date.

I'm a little confused by this. If someone could just help me.

Answer

Anu picture Anu · Dec 19, 2013

Make the following changes in your code

final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
                    // when dialog box is closed, below method will be called.
                    public void onDateSet(DatePicker view, int selectedYear,
                            int selectedMonth, int selectedDay) {
                        if (isOkayClicked) {
                            birthday.setText(selectedYear + (selectedMonth + 1)
                                    + selectedDay);
                            yearStr = selectedYear;
                            monthStr = selectedMonth;
                            dayStr = selectedDay;
                        }
                        isOkayClicked = false;
                    }
                };
                final DatePickerDialog datePickerDialog = new DatePickerDialog(
                        RegistrationTwoActivity.this, datePickerListener,
                        yearStr, monthStr, dayStr);

                datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                        getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if (which == DialogInterface.BUTTON_NEGATIVE) {
                                    dialog.cancel();
                                    isOkayClicked = false;
                                }
                            }
                        });

                datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
                        "OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if (which == DialogInterface.BUTTON_POSITIVE) {
                                    isOkayClicked = true;
                                    DatePicker datePicker = datePickerDialog
                                            .getDatePicker();
                                    datePickerListener.onDateSet(datePicker,
                                            datePicker.getYear(),
                                            datePicker.getMonth(),
                                            datePicker.getDayOfMonth());
                                }
                            }
                        });
                datePickerDialog.setCancelable(false);
                datePickerDialog.show();

To work this code, you should change your minSdkVersion to atleast 11 in Manifest. Hope this will help you.. :)