Home > Software engineering >  android studio prevent past date in datepicker kotlin
android studio prevent past date in datepicker kotlin

Time:03-20

How can I disable only past date days from datepicker and not the future? I know it is somewhere around the startCalendar.set but I can't find the setMinValue.

/* Calendar Start Date Button Click */
        startdatepicker = findViewById(R.id.CalendarStartImage)
        startdateview = findViewById(R.id.StartDateTextView)
        val startCalendar = Calendar.getInstance()

        val startPicker = DatePickerDialog.OnDateSetListener{ view, year, month,dayOfMonth ->
            startCalendar.set(Calendar.YEAR,year)
            startCalendar.set(Calendar.MONTH,month)
            startCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth)
            

            updateStartCalendar(startCalendar)
        }
        startdatepicker.setOnClickListener{
            DatePickerDialog(this,startPicker,startCalendar.get(Calendar.YEAR),startCalendar.get(Calendar.MONTH),
                startCalendar.get(Calendar.DAY_OF_MONTH)).show()
        }

CodePudding user response:

Try replacing content of setOnClickListener with this something like this

val dialog = DatePickerDialog(this,startPicker, startCalendar.get(Calendar.YEAR), startCalendar.get(Calendar.MONTH), startCalendar.get(Calendar.DAY_OF_MONTH))
dialog.datePicker.minDate = startCalendar.getTimeInMillis()
dialog.show()
  • Related