‫Welcome back.
‫In this video we're going to learn about date pickers.
‫A date picker in Android is a UI component that allows users to select a date.
‫It provides a calendar like interface, allowing users to choose a day, month, and year.
‫The selected date can be useful in various scenarios, such as setting reminders, scheduling events,
‫or any application that requires handling date input.
‫To create a date picker.
‫Let's go to the layout.
‫Start with date picker tag wrap content, wrap content, and here I'll specify an ID which is the date
‫picker.
‫And here we go.
‫This is the calendar.
‫I'll remove the time picker, I'll comment it, and then drag and drop this date picker below and infer
‫the constraints.
‫Here we go.
‫This is our date picker.
‫Let's move to the main activity.
‫And here I'll comment.
‫This display time picker method goes no longer.
‫I can find the time picker widget.
‫I'll create a new function.
‫I'll name it as Display date picker.
‫Let me initialize that date picker equals to find view by ID you can pass the type here date picker
‫r.id dot date picker.
‫You can set a listener to be notified when the user changes the date.
‫Using the set on date changed listener method.
‫So date picker dot set on date changed listener.
‫Here I'll select this with a lambda expression.
‫We have four parameters date picker, I, i2, and i3.
‫The first parameter is date picker which is the view, and in this case the date picker itself I, which
‫refers to the year for the selected year, i2 refers for the selected month of the year, and i3 refers
‫for the day of month that is selected by the user.
‫I'll create a toast message.
‫And here we go.
‫But there is a very important topic here.
‫I need to add a requires API o which is level 26 and above.
‫Click Add Annotation.
‫And if we notice that this method is marked with this annotation requires API bundle dot version codes
‫dot zero, which is 26 and above goes date.
‫Pickers are used with higher API levels, so at requires API is an annotation used in Android to indicate
‫the minimum API level required for a particular method or class.
‫In Kotlin, this annotation is applied to functions, methods, or classes to specify the minimum API
‫level required for them to run.
‫Let's call this function in the Oncreate method.
‫Run the application and this is our date picker.
‫I'll select 10th August 2023.
‫Year 23 month is seven and day is ten.
‫You need to add one for the month for the selected month because counting month starts from zero in
‫date picker.
‫So it's very simple.
‫You can add plus one to this using the string template again August 10th August.
‫And you can see month is eight.
‫Okay.
‫This is how we create date pickers in Android, how we handle the click events and the selection on
‫the date pickers.