I'm trying to get the values from DatePicker, but I've no idea how could I manage it.
My layout contains a DatePicker element, and I'm using kotlin.
Same problem with TimePicker.
I'm trying to get the values from DatePicker, but I've no idea how could I manage it.
My layout contains a DatePicker element, and I'm using kotlin.
Same problem with TimePicker.
Can you be more specific? Do you want to show DatePicker and TimePicker to user and get the user selected value? In that case, you can try like this. For DatePicker,
val mCalendar = Calendar.getInstance()
val mYear = mCalendar.get(Calendar.YEAR)
val mMonth = mCalendar.get(Calendar.MONTH)
val mDay = mCalendar.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(
this, DatePickerDialog.OnDateSetListener { _, thisYear, thisMonth, thisDay ->
mYear = thisYear
mMonth = thisMonth
mDay = thisDay
tDate.text = mDay.toString() + "/" + (mMonth + 1).toString() +
"/" + mYear.toString()
}, mYear, mMonth, mDay
)
dpd.show()
For TimePicker,
TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { _, hourOfDay, minute ->
tvHour.text = Calendar.HOUR_OF_DAY, hourOfDay
tvMinute.text = Calendar.MINUTE, minute
}, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false).show()
Thanks for Márton Soós
This helper class does the magic for both DatePicker and TimePicker:
Than you can call it: