Android DatePickerDialog Show only current month remain dates selectable

691 Views Asked by At

I am trying to show only the current month with the remaining dates selectable in Date Dialog Piker. My Code:

val calendar = Calendar.getInstance()
//getting current day,month and year.
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)

val datePickerDialog =
    DatePickerDialog(
        context, DatePickerDialog.OnDateSetListener
        { view, year, monthOfYear, dayOfMonth ->
            val currentDate: String = "$year-${(monthOfYear + 1)}-$dayOfMonth"

        }, year, month, day
    )

// Max = current
val maxTime = calendar.timeInMillis

// Move day as first day of the month
calendar.set(Calendar.MONTH, 1)
// Move "month" for previous one
calendar.add(Calendar.MONTH, 1)

// Min = time after changes
val minTime = calendar.timeInMillis
datePickerDialog.datePicker.maxDate = maxTime
datePickerDialog.datePicker.minDate = System.currentTimeMillis()
datePickerDialog.show()

It's showing the current month and current date selectable but future dates are also disabled. I only want previous dates to be disabled and future remaining dates can be selectable of current month.

1

There are 1 best solutions below

2
raghuram sagili On

You are clearly setting maxDate minDate to present day so all the other days in calendar was blocked.

val minTime = calendar.timeInMillis

datePickerDialog.datePicker.maxDate = maxTime

// above for maxTime you assigned current date don't provide maxDate datepickerdialog remove above line and check

datePickerDialog.datePicker.minDate = System.currentTimeMillis()
datePickerDialog.show()