Solution 1 :
It is a known bug. There is a ticket recorded 2020-09-29 and still open (in assigned state) as of 2022-02-08.
A similar bug tracker entry with the same issue created 2019-02-19 was closed on 2019-12-04 as “won’t fix” due to it being low priority.
I am sorry to say this, but I believe this is not going to be fixed.
However, what you can do is extend the android.app.DatePickerDialog
to introduce a fix yourself:
class MyDatePickerDialog(context: Context) : DatePickerDialog(context) {
override fun onDateChanged(view: DatePicker, year: Int, month: Int, dayOfMonth: Int) {
val newDate = Calendar.getInstance().apply { set(year, month, dayOfMonth) }
val minDate = Date(datePicker.minDate)
val maxDate = Date(datePicker.maxDate)
newDate.time = minOf(maxDate, maxOf(minDate, newDate.time))
super.onDateChanged(
view,
newDate.get(Calendar.YEAR),
newDate.get(Calendar.MONTH),
newDate.get(Calendar.DAY_OF_MONTH)
)
}
}
Solution 2 :
Please try Date().getTime()
instead of System.currentTimeMillis()
DatePickerDialog(
activity,
OnDateSetListener { _: DatePicker?, year: Int, monthOfYear: Int, dayOfMonth: Int ->
selectedBirthday.set(year, monthOfYear, dayOfMonth)
},
1980, 7, 2
).apply {
datePicker.maxDate = Date().getTime() // max date = today
}.show()
Problem :
I have created a DatePickerDialog and I am trying to limit the picker to the max date of “today” (currently: 2020, 13 Jul), like this:
DatePickerDialog(
activity,
OnDateSetListener { _: DatePicker?, year: Int, monthOfYear: Int, dayOfMonth: Int ->
selectedBirthday.set(year, monthOfYear, dayOfMonth)
},
1980, 7, 2
).apply {
datePicker.maxDate = System.currentTimeMillis() // max date = today
}.show()
The dialog opens correctly with default data (“1980, Aug 02”).
However, I have found a bug when “1980, Aug 02” is selected and I change the year to 2020: The dialog then shows “2020, Aug 02” on top ignoring the maxDate limit setup on the datePicker, but the calendar below is correctly limited to July 13.
If I click on “OK” button, the year, monthOfYear and dayOfMonth returned on my DateSetListener are 2020, Aug, 02, which is a future date from what I wanted. Is there any workaround to avoid this bug on the DatePickerDialog with the maxDate being ignored?
Comments
Comment posted by Shalu T D
Please try my answer. Let me know if it’s not worked?
Comment posted by Jenea Vranceanu
I haven’t been able to reproduce the issue. One thing you can try is to update selected date right below the line where you set the maximum date inside of
Comment posted by user3429953
@JeneaVranceanu Feel free to copy and paste the code to reproduce it! The steps when the dialog appears are: 1. Select a past year (for example: 1980) 2. Select a “future” month&day (for example: 02, Sept) 3. Select the current year (2020)
Comment posted by user3429953
@ShaluTD Sorry, it didn’t…
Comment posted by AplusKminus
I know this doesn’t help, but at least I can provide more details: I have just reproduced this bug in an app I am developing. It appears on Android 11 (RSR1.201013.001), but not on android 12 (SQ1A.220105.002).
Comment posted by Jenea Vranceanu
Date()
Comment posted by user3429953
It did not work but thanks for the try! It seems that the maxDate is setup correctly since it is blocked on the calendar and it does not allow me to check a future day or month if I’m on 2020 year. But if I select a “future month” in a past year and then change to the current year, the “bug” shows up.