Different attributes of DatePicker control –

 

XML Attributes Description
android:id Used to uniquely identify the control.
android:datePickerMode Used to specify the mode of datepicker(spinner or calendar)
android:calendarTextColor Used to specify the color of the text.
android:calendarViewShown Used to specify whether view of the calendar is shown or not.
android:background Used to set background color of the Text View.
android:padding Used to set the padding from left, right, top and bottom.

 

DatePicker in Kotlin

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: 
 

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. Then select the Empty activity => next => finish.

 

Similar Reads

Android DatePicker with Calendar mode

We can use android:datePickerMode to show only calendar view. In the below example, we are using the DatePicker in Calendar mode....

Android DatePicker with Spinner mode

...

Different attributes of DatePicker control –

We can also show the DatePicker in spinner format like selecting the day, month and year separately, by using android:datePickerMode attribute and set android:calendarViewShown=”false”, otherwise both spinner and calendar can be seen simultaneously....

To use Calendar DatePicker in activity_main.xml

...

To use Spinner DatePicker in activity_main.xml

...

Modify the strings.xml file to add the string-array

In this file, we will add the DatePicker and Button widget and set their attributes so that it can be accessed in the kotlin file....

Access the DatePicker in MainActivity.kt file

...

AndroidManifest.xml file

In this file, we will add the DatePicker and Button widget and set their attributes so that it can be accessed in the kotlin file....

Run as Emulator:

...

Contact Us