I have an issue in time picker dialog , can i disable past time in android time picker dialog
Want to diasble past time in time picker
CodePudding user response:
try this code
TimePickerFragment timePickerFragment = new TimePickerFragment();
timePickerFragment.setOnTimeSetListener(new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar datetime = Calendar.getInstance();
Calendar c = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.set(Calendar.MINUTE, minute);
if (datetime.getTimeInMillis() >= c.getTimeInMillis()) {
//it's after current
int hour = hourOfDay % 12;
btnPickStartTime.setText(String.format("d:d %s", hour == 0 ? 12 : hour,
minute, hourOfDay < 12 ? "am" : "pm"));
} else {
//it's before current'
Toast.makeText(getApplicationContext(), "Invalid Time", Toast.LENGTH_LONG).show();
}
}
});
timePickerFragment.show(getSupportFragmentManager(), "TIME");
CodePudding user response:
Try this code... MaterialDatePicker
val calendar = Calendar.getInstance()
val constraintsBuilder = CalendarConstraints.Builder()
val dateValidator: DateValidator = DateValidatorPointForward.from(calendar.timeInMillis)
constraintsBuilder.setValidator(dateValidator);
val builder = MaterialDatePicker.Builder.datePicker()
val picker = builder
.setSelection(calendar.timeInMillis)
.setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
.setCalendarConstraints(constraintsBuilder.build())
.build()
picker.show(supportFragmentManager, "")
CodePudding user response:
Yes, you can disable past time in the Android TimePickerDialog by using the setMinTime() method. This method takes a long value representing the minimum time that can be selected in the TimePickerDialog. For example, if you want to disable past time, you can set the minimum time to the current time by calling setMinTime(System.currentTimeMillis()).
Here is an example of how to use the setMinTime() method to disable past time in a TimePickerDialog in Android:
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the selected time
}
}, currentHour, currentMinute, true);
timePickerDialog.setMinTime(System.currentTimeMillis());
timePickerDialog.show();