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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With