I am using android's TimePickerDialog this way
val tpd = TimePickerDialog(this, { dialog, h, min ->
// I want to dismiss the dialog only if the validations succeed
}, 12, 15, false)
tpd.show()
How do I prevent it from getting auto dismissed after user selects a time? I want to run my validations before I can dismiss the time picker dialog.
Since
TimePickerDialogextendsAlertDialogyou can get the positive Button and modify its click listener This way.But the problem here is you will not access to the
TimePickerwidget because you need it to get the time for validation. Becasue if you override the positive button click your listener will not get called andmTimePickerfield is private inTimePickerDialog. Sure you can access it by reflection but that's not right way to do stuff .As a solution create a Custom
AlertDialog. that way you can have control over selection.Use This Approach to prevent
AlertDialogfrom getting closed and you are good to go.