23

I am trying to create a DatePickerDialog in my app in Android but when I create a DatePickerDialog I receive the following message: Call requires API level 24 (current min is 14): android.app.DatePickerDialog#DatePickerDialog

How can I use a DatePickerDialog in old API versions?

GuiDupas
  • 1,681
  • 2
  • 22
  • 44

2 Answers2

46

Use one of the constructors added in API level 1, not one of those added in API level 24.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Here is an example that works from API level 1:

val cal = Calendar.getInstance()   // used for initializing the date picker
val datePickerDialog = DatePickerDialog(requireContext(), DatePickerDialog.OnDateSetListener { datePicker, y, m, d ->

    // get user's selected y, m and d here

},
    cal.get(Calendar.YEAR),
    cal.get(Calendar.MONTH),
    cal.get(Calendar.DAY_OF_MONTH)
)
datePickerDialog.show()