I have an activity - TimePickerActivity - which creates a TimePickerDialog. I have a onTimeSetListener which responds to the Set button at the end of which it calls finish() and returns to the activity that called the TimePickerActivity. I want it to finish and return if the Cancel button is clicked but can't find any way to do this. I have googled and tried out severall suggestions but none of them seem to work. Any simple way to do this?
6 Answers
Just pass an onCancelListener to TimePicker.setOnCancelListener()
edit: After Ron's problems with implementation I decided to actually test myself the code (I answered just looking at the API) and I discovered than even if the code is correct (I suppose you had a typo somewhere as my code compiles ok), when clicking the cancel button it didn't respond as intended...
It appears that when you click cancel
button the Dialog doesn't call the cancel()
method that fires the OnCancelListener
as it would seem the obvious, but the dismiss()
method that fires an OnDismissListener
, pretty weird...
So this code is working fine for me:
TimePickerDialog.OnTimeSetListener mTimeSetListener = new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//time set stuff
}
};
TimePickerDialog myTPDialog = new TimePickerDialog(this,mTimeSetListener,0,0,false);
myTPDialog.setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
// Cancel code here
}
});
myTPDialog.show();
all credits to this SO answer...
-
TimePickerDialog myTPDialog = new TimePickerDialog(this,mTimeSetListener,0,0,false); OnCancelListener ocl = new OnCancelListener() { public void onCancel(DialogInterface arg0) { } }; TimePickerDialog.setOnCancelListener( ocl); – ron Jan 18 '11 at 16:01
-
error with :- Syntax error on token "ocl", VariableDeclaratorId expected after this token and Syntax error on token(s), misplaced constructs – ron Jan 18 '11 at 16:04
-
I need to amend the above but cant find an edit facility and is there a better way to enter code and why does this comment close when a type a cr. Sorry if this seems obvious but not to me. – ron Jan 18 '11 at 16:10
-
replace `TimePickerDialog.setOnCancelListener(ocl);` with `myTPDialog.setOnCancelListener(ocl);` – maid450 Jan 18 '11 at 16:26
-
maid: that produces Syntax error on token "ocl", VariableDeclaratorId expected after this token Syntax error on token(s), misplaced construct(s) – ron Jan 19 '11 at 15:47
-
maid: that did it - many thanks. I looked again in the android developer reference and the section dealing with onDismiss and OnCancel don't seem to tally with this succesful solution! – ron Jan 19 '11 at 18:18
-
8Doesn't the onDismiss listener also get triggered when the ok button is clicked? – Fortega Jun 01 '12 at 09:41
-
After seeing binW's answer, I was able to finally write a running solution. Check here: https://stackoverflow.com/questions/4724781/timepickerdialog-cancel-button/47043247#47043247 – Deepti Oct 31 '17 at 19:35
here is how I did it:
TimePickerDialog tp = new TimePickerDialog(this, mTimeSetListener, 0, 0, false);
tp.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
if (which == DialogInterface.BUTTON_NEGATIVE)
{
tbTimer.setChecked(false);
}
}
});

- 13,220
- 11
- 56
- 69
-
It works, but when clicked outside a dialog, a click event is not catched. – CoolMind Feb 20 '16 at 14:40
-
Thanks binW! You showed me the path and I reached the destination. Check here: https://stackoverflow.com/questions/4724781/timepickerdialog-cancel-button/47043247#47043247 – Deepti Oct 31 '17 at 19:36
TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
//ok button clicked
}
};
Calendar c = Calendar.getInstance();
int now_hour = c.get(Calendar.HOUR_OF_DAY);
int now_minutes = c.get(Calendar.MINUTE);
final TimePickerDialog timePickerDialog = new TimePickerDialog(this, timePickerListener, now_hour, now_minutes + 1, false);
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//cancel button clicked
}
});
timePickerDialog.show();

- 36,322
- 27
- 84
- 93

- 1,225
- 12
- 24
-
If you timePickerDialog.setCancelable(false), then the user must explicitly press ok or cancel, which prevents cancelling on dismiss. – Luís Henriques May 05 '20 at 12:02
My solution is based on @maid450 answer, remark of @Fortega and a fact that OnDismissListener is called (usually in last turn) on any click when a time picker dialog is shown.
private int timeSetStatus; // A clicked button result.
...
timeSetStatus = 0;
final Calendar calendar = Calendar.getInstance();
final TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
timeSetStatus = 1;
// Other actions.
}
}, calendar.get(Calendar.HOUR_OF_DAY), 0, true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
timeSetStatus = 2;
// Actions on clicks outside the dialog.
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (timeSetStatus == 0) {
// Actions on Cancel button click.
}
}
});
dialog.show();

- 26,736
- 15
- 188
- 224
I've just override (kotlin sample code)
override fun onCancel(dialog: DialogInterface?) {
super.onCancel(dialog)
callback?.onCancelClick()
}
method inside my DialogFragment and called onCancelClick of my interface:
interface SetAlarmTimeListener: TimePickerDialog.OnTimeSetListener {
fun onCancelClick()
}

- 3,405
- 1
- 24
- 32
Finally a running solution...
Declare a boolean variable in class as below:
private boolean isTimeSelected;
Then on button click call the following method:
public void onButtonClicked() {
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP || isTimeSelected) {
lastSelectedHour = selectedHour;
lastSelectedMinute = selectedMinute;
// perform OK button tasks
}
}
}, lastSelectedHour, lastSelectedMinute, true);
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
// perform CANCEL button tasks
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
timePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
isTimeSelected = true;
}
}
});
timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
isTimeSelected = false;
}
}
});
timePickerDialog.setCancelable(false);
timePickerDialog.setTitle("Select Time");
}
timePickerDialog.show();
}

- 934
- 12
- 18