Search code examples
androidandroid-timepicker

TimePickerDialog : Clicking "Cancel" button calls onTimeSet() method


I use TimePickerDialog with onTimeSet() method. I have added a cancel button with onClickListener.
My problem is that when I click cancel button, onTimeSet() method is called. Why is it? Any way to solve this issue?

// Create timePicker and set listener
timeListener = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {


        };

    timePicker = new TimePickerDialog(RS_CreateTripActivity.this,
                timeListener, hour, min, false);
    timePicker.setCanceledOnTouchOutside(false);
    timePicker.setCancelable(false);

    // Set negative button click listener
    timePicker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
                        datePicker.dismiss();
        }
});

I tried by setting positive button with onClickListener but in that case I could not get time selected by user. If any one tells me how to get time within onClickListener, then it would also solve my problem.


Solution

  • There is an issue with the DatePickerDialog and the TimePickerDialog (Issue #34833). You could use a counter as a work-around to get rid of this issue. Pls. refer to : Date Picker with cancel option in android