Skip to content Skip to sidebar Skip to footer

Datepicker In 4.3 Is Setting The Date Even When Clicked On Cancel Button

I have a datepicker. The datepicker is working fine on all devices except android 4.3. When i click on cancel button of datepicker is setting the date instead of cacelling itself.

Solution 1:

Set the buttons of the datepicker to avoid this bug. Here is an example.

Calendar newCalendar = Calendar.getInstance();
        final DatePickerDialog datepicker = new DatePickerDialog(c,null, newCalendar.get(Calendar.YEAR),
                newCalendar.get(Calendar.MONTH),
                newCalendar.get(Calendar.DAY_OF_MONTH));
        datepicker.setCancelable(true);
        datepicker.setCanceledOnTouchOutside(true);
        datepicker.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Calendar newDate = Calendar.getInstance();
                         int dayOfMonth = datepicker.getDatePicker().getDayOfMonth();
                         int monthOfYear = datepicker.getDatePicker().getMonth() ;
                         int year = datepicker.getDatePicker().getYear();

                        newDate.set(year, monthOfYear, dayOfMonth);
                        tv.setText(CommonDateFunction.format(newDate.getTime(),
                                CommonDateFunction.FORMAT_DD_MMM_YYYY));
                    }
                });
        datepicker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                       // Log.d("Picker", "Cancel!");
                    }
                });
        datepicker.show();

Post a Comment for "Datepicker In 4.3 Is Setting The Date Even When Clicked On Cancel Button"