Android DatePicker is a user interface control which is used to select the date by day, month and year in our android application. DatePicker is used to ensure that the users will select a valid date.
In android DatePicker having two modes, first one to show the complete calendar and second one shows the dates in spinner view.
We can create a DatePicker control in two ways either manually in XML file or create it in Activity file programmatically.
First we create a new project by following the below steps:
- Click on File, then New => New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- Then select the Empty activity => next => finish.
Modify activity_main.xml file
Modify strings.xml file
Create the DatePicker in MainActivity.kt file
First of all, we declare a variable datePicker to create the DatePicker widget in Kotlin file.
val datePicker = DatePicker(this)
then, we will add the datePicker in the linearLayout using
val linearLayout = findViewById
(R.id.linear_layout) // add datePicker in LinearLayout linearLayout?.addView(datePicker)
We are familiar with further activities in previous articles.
Run as Emulator:
- DatePicker in Kotlin
- Dynamic TextSwitcher in Kotlin
- Dynamic ImageSwitcher in Kotlin
- Dynamic EditText in Kotlin
- Dynamic RadioButton in Kotlin
- Dynamic ProgressBar in Kotlin
- Dynamic Switch in Kotlin
- Dynamic CheckedTextView in Kotlin
- Dynamic TextView in Kotlin
- Dynamic Button in Kotlin
- Dynamic ImageView in Kotlin
- Dynamic ScrollView in Kotlin
- Dynamic RadioGroup in Kotlin
- Dynamic Spinner in Kotlin
- Dynamic AutoCompleteTextView in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.