Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Material Design Date Picker in Android using Kotlin

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. If you like the way how the UI elements from Google Material Design Components for android which are designed by Google are pretty awesome, then here are some steps that need to be followed to get them, and one of them is Google Material Design Components (MDC) Date Picker. There are a lot of date pickers available for Android which are Open Source. But the Material design date pickers offer more functionality to the user and are easy to implement for developers. Have a look at the following images on what type of material design date pickers are going to be discussed in this discussion. Note that we are going to implement this project using the Java language. In this article, we are going to implement two types of material design date pickers as one can see in the below images.

  • Material Design Normal Date Picker
  • Material Design Date Range Picker

Material Design Date Picker

Skeleton of Date Picker Dialog Box

Before going to implement the material design date picker, understanding the parts of the dialog box is necessary so that it can become easier while dealing with parts of the dialog box in Kotlin code.

Skeleton of Date Picker Dialog Box

Step by Step Implementation

Step 1: Create a new project in android studio using kotlin.

Step 2: Add dependency Material Components for the Android library in build.gradle file.

implementation ''

Note: In the latest version of android studio, material dependency is already included in build.gradle file.

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. You can customize the design part. 


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintVertical_bias="0.276" />
        app:layout_constraintTop_toTopOf="parent" />

Note: We have also included vector images in the drawable folder, if you want to use ImageView, you need to add vector image for that.

Step 4: Working with MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package com.ayush.datepicker
import android.os.Bundle
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
    lateinit var btnDatePicker: FloatingActionButton
    override fun onCreate(savedInstanceState: Bundle?) {
        // setting up the views
        btnDatePicker = findViewById(
        // when floationg acition button is clicked
        btnDatePicker.setOnClickListener {
            // Initiation date picker with 
            // MaterialDatePicker.Builder.datePicker()
            // and building it using build()
            val datePicker = MaterialDatePicker.Builder.datePicker().build()
  , "DatePicker")
            // Setting up the event for when ok is clicked
            datePicker.addOnPositiveButtonClickListener {
                // formatting date in dd-mm-yyyy format.
                val dateFormatter = SimpleDateFormat("dd-MM-yyyy")
                val date = dateFormatter.format(Date(it))
                Toast.makeText(this, "$date is selected", Toast.LENGTH_LONG).show()
            // Setting up the event for when cancelled is clicked
            datePicker.addOnNegativeButtonClickListener {
                Toast.makeText(this, "${datePicker.headerText} is cancelled", Toast.LENGTH_LONG).show()
            // Setting up the event for when back button is pressed
            datePicker.addOnCancelListener {
                Toast.makeText(this, "Date Picker Cancelled", Toast.LENGTH_LONG).show()

So our app is ready and we can see the output.


My Personal Notes arrow_drop_up
Last Updated : 28 Jan, 2022
Like Article
Save Article
Similar Reads