Skip to content

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 in your app very easily. Some of the examples of the app are 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 the 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 it in the TextView label or elsewhere your want to use it. Let’s 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.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Open Date Picker"
        android:onClick="clickDataPicker"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>
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
package `in`.eyehunt.androiddatepickerdialog

import android.app.DatePickerDialog
import android.icu.util.Calendar
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.view.View
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    fun clickDataPicker(view: View) {
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)

        val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
            // Display Selected date in Toast
            Toast.makeText(this, """$dayOfMonth - ${monthOfYear + 1} - $year""", Toast.LENGTH_LONG).show()

        }, year, month, day)
        dpd.show()
    }
}
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

Your email address will not be published. Required fields are marked *