Material-components-android: MaterialDatePicker Validators

Created on 8 Nov 2019  路  3Comments  路  Source: material-components/material-components-android

MaterialDatePicker

Is there a way to set a validator for the previous days from today? Such that all days after today are invalid. In a scenario where the date picker is used for selecting dates for a report. You can get reports for the future

question

Most helpful comment

@KelvinPac You can use this:

@Parcelize
class BirthdateSelectionValidator : CalendarConstraints.DateValidator {
    override fun isValid(date: Long): Boolean = date <= System.currentTimeMillis()
}

All 3 comments

I'm not super familiar with DatePicker component but have you tried with CalendarConstraint class?

In the catalog sample app there is a DateValidatorWeekDays which uses the DateValidator defined inside and it seems customizable for your own logic (check the isValid method)

Don't know if this can be helpful to your use case but you could try if you didn't yet

That's correct, you should be able to use isValid in CalendarConstraint

@KelvinPac You can use this:

@Parcelize
class BirthdateSelectionValidator : CalendarConstraints.DateValidator {
    override fun isValid(date: Long): Boolean = date <= System.currentTimeMillis()
}
Was this page helpful?
0 / 5 - 0 ratings