ron
ron

Reputation: 5279

TimePickerDialog cancel button

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?

Upvotes: 4

Views: 11559

Answers (6)

Deepti
Deepti

Reputation: 954

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();
}

Upvotes: 0

Yessine Mahdouani
Yessine Mahdouani

Reputation: 1264

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();

Upvotes: 2

Anton Derevyanko
Anton Derevyanko

Reputation: 3445

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()
}

Upvotes: 0

CoolMind
CoolMind

Reputation: 28793

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();

Upvotes: 0

binW
binW

Reputation: 13692

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);
        }
    }
});

Upvotes: 4

maid450
maid450

Reputation: 7486

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...

Upvotes: 9

Related Questions