Android DatePicker


In order to provide user with date selection, we need to use – Android DatePicker. This tutorial will cover on how to get current-date, how to invoke Date-Picker Dialog-box and how to set the selected date to EditText.


Source Code :


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) DatePicker which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/DatePicker/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.




Explaining above Code :

  • Line 31-35 So as to get Current date we have made instance of class Calendar, and using this we stored day, month & year in respective variables.
  • Line 43 Whenever user clicks on our button  update_date() method will be invoked.
  • Line 46 On button-click we call a Dialog-Box which contains a DatePicker using showDialog(0) i.e. below Dialog declared on Line-53. ( You should not worry about the value 0, passed as a parameter since it worth full in case of multiple dialog )
  • Line 53-56 We declare a Dialog having Date-Picker & at Line-55 we pass context(this), datePickerListener & current date which we stored using calendar class on Line-31.
  • Line 61-69 When user selects a date from datePicker Dialog, this event will be caught by dateSetListener at Line-61. Inside this listener we have a method onDateSet() where we set Date selected by user to our edittext Line-67.



2. activity_main.xml : Layout file

  • Inside our activity_main.xml we have added a Button which will invoke  update_date() method which brings a dialog having Date-Picker. And we have placed a EditText which displays Date selected by  user from Date-Picker.



Final Output :

Android DatePicker
                                  Android DatePicker



Report Errors + Bugs & Become Insider for

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to you could help other peers.