5

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.

Geek
  • 8,280
  • 17
  • 73
  • 137
  • 1
    It first enters `onClick` method and then `onTimeSet`. Thus, used a boolean variable to check if it entered `onClick` method. And it did, then don't execute any code in `onTimeSet` method. – Geek Jul 01 '13 at 09:49
  • Please find running solution here: https://stackoverflow.com/questions/4724781/timepickerdialog-cancel-button/47043247#47043247 – Deepti Oct 31 '17 at 19:32

2 Answers2

2

You have to used this way.

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;
                    time = ""+mHour+":"+mMinute;

           }
    };



@Override
   protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            TimePickerDialog myTime = new TimePickerDialog(this,mTimeSetListener, h, m, false);
            myTime(new DialogInterface.OnCancelListener(){
            @Override
            public void onCancel(DialogInterface arg0) {
                //your cancel task.
            }
            );//For cancel button

        return myTime; //Return the dialog
        break;
        return null;
    }

It is work like charm.

Harshid
  • 5,701
  • 4
  • 37
  • 50
  • I don't extend `DialogFragment`. Will the `onCreateDialog` method work then? Is `TIME_DIALOG_ID`, your variable or declared in SDK? – Geek Jul 01 '13 at 06:52
  • it is declary inbuilt in sdk so what is problem? – Harshid Jul 01 '13 at 09:10
1

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

Community
  • 1
  • 1
lokoko
  • 5,785
  • 5
  • 35
  • 68