Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] Android DatePicker bug on maxDate

Posted on November 14, 2022

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.

Screenshot:
Android DatePickerDialog ignores maxDate

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?

READ  [ANSWERED] java - High resolution video file not playing in exoplayer
Powered by Inline Related Posts

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.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress