Android Date Picker Dialog with Example in Kotlin

Android Date Picker Dialog allows users to select the date consisting of day, month and year in app date picker dialog user interface. Android provides DatePicker and DatePickerDialog components to get done this in your app very easily. Some of example of app is date picker use in Railway ticket booking app or flight booking or hotel , holidays etc (MakeMyTrip , Booking.com , Trivago etc)

Android Date Picker Dialog with Example in Kotlin Android DatePickerDialog

In this tutorial, you will learn following:

  1. Overview Android Date Picker Dialog 
  2. Complete example and code of Android DatePickerDialog in kotlin

 

Let’s Build Android Android Date Picker Dialog example in kotlin:

In the kotlin showing DatePickerDialog is very easy, in this example see how much easy. In the example the selected data will show in Toast. You can Show in TextView label or else where your want to use it. Let’t see the steps to how to do it ..

 

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Add following code in “activity_main.xml” layout file

Where button have method to perform a onClick action.

Step 3. Open the “MainActivity.kt” and add following code

On clicking button – Creates a new date picker dialog for the current date using the parent context’s default date picker dialog theme. Context is requires the application context.

  • var year : It shows the the current year that’s visible when the dialog pops up
  • var month : It shows the the current month that’s visible when the dialog pops up
  • var dat : It shows the the current day that’s visible when the dialog pops up

Step 4. Now Run the application, in emulator or On you android device
Output screenshot Android Date Picker Dialog :

 

Android Date Picker Dialog with Example in Kotlin Android DatePickerDialog output

 

Download source code Android DatePickerDialog in kotlin

https://github.com/eyehunt/AndroidDatePickerDialog

 

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


4 thoughts on “Android Date Picker Dialog with Example in Kotlin

  1. i have 2 date picker. One is for the start date and the another is for finish date. How can i disable past dates from the start day.

    1. You can use DatePicker#setMinDate() function to set a minimum date.
      datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());

    1. datePicker.setMinDate(System.currentTimeMillis() – 1000);
      Or
      if you are using an object of DatePickerDialog you can do

      datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() – 1000);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.