0

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

  • this might help https://stackoverflow.com/questions/41506715/android-timepickerdialog-prevent-user-select-past-time-and-can-select-future-ti – Devesh Kumawat Jan 25 '23 at 07:47
  • Thanx for reply sir, i know this but i want to disable past time like date picker, I mean user can not select the past time, the time should be disable , is it possible or not sir , a – Coop Cab Jan 25 '23 at 08:04

2 Answers2

1

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("%02d:%02d %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");
MANIMARAN
  • 11
  • 4
  • Thanx sir , but i want to disable previous time , like the time is 2:00 pm and I want if user want to click on 1:59 or any previous time it should not clickable , is it possible in timepickerdialog – Coop Cab Jan 25 '23 at 08:06
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 26 '23 at 09:44
0

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, "")
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23