Reputation: 183
I am using DatePickerDialog in my Android project using Kotlin. I have successfully created DatePickerDialog and its working fine. Now I want to give some validation like in DatePickerDialog I want to disable future date. For Example, Today date is 22 Aug 2019 I want to disable all date after today date.
I tried to use dateSetListener.getDatePicker().setMaxDate(System.currentTimeMillis())
but it's not working
var cal = Calendar.getInstance()
// create an OnDateSetListener
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(
view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int
) {
cal.set(Calendar.YEAR, year)
cal.set(Calendar.MONTH, monthOfYear)
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
updateDateInView()
}
// dateSetListener.getDatePicker().setMaxDate(calendar.getTimeInMillis());
}
// when you click on the button, show DatePickerDialog that is set with OnDateSetListener
button_date!!.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View) {
DatePickerDialog(this@MainActivity,
dateSetListener,
// set DatePickerDialog to point to today's date when it loads up
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)).show()
}
})```
Upvotes: 1
Views: 1773
Reputation: 538
Try this
var dialog = DatePickerDialog(
this@MainActivity,dateSetListener,
// set DatePickerDialog to point to today's date when it loads up
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)
)
dialog.datePicker.maxDate = Calendar.getInstance().timeInMillis
dialog.show()
Upvotes: 1