Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timepicker without cancel button?

How do you create an Android timepicker without a cancel button? I would had expected setCancelable(false) would do this, but does not get rid of the button, it only seems to prevent cancellations from clicking outside of the window or back button.

like image 220
Derek Seabrooke Avatar asked Jan 21 '26 05:01

Derek Seabrooke


1 Answers

If you'd like to use the TimePickerDialog, it is pretty simple. You just simply need to detect when the dialog is shown, look up the NEGATIVE button since that serves the "Cancel" one and set it's visibility to GONE..like this:

final TimePickerDialog timePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
         // Do something with the time
    }
}, 12, 25, true); // 12 is the hour and 25 is minutes..please change this

timePicker.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        // This is hiding the "Cancel" button:
        timePicker.getButton(Dialog.BUTTON_NEGATIVE).setVisibility(View.GONE);
    }
});
timePicker.show();
like image 86
Mike Avatar answered Jan 22 '26 20:01

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!