How to create Date Picker dialog for selecting a date in Android

Whenever the user has to input a date, its always a good idea to assist him/her with a widget which allows the user to select a particular day, month and year.

By default Android provides a widget named DatePicker which can be incoported in your application to provide this functionality to the end users. It has an elegant look and design. So, we will use this widget here.

Android 3.2

Note: We will be using Android Platform 3.2 for developing our application. 

To demonstrate how to use DatePicker widget we will create a layout which will have a textview and button. The button will be used to choose a new date and once chosen the updated date will be displayed in the textview. Create a new Android project with the following properties:

  • Project Name – Date Picker
  • Name of the Application – Date Picker
  • Package name – com.botskool.DatePicker
  • Activity Name – DatePickerActivity

Create a new XML file in res/layout/ and name it as main.xml and copy the following code to it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:text="Date will appear here after being selected"
<Button android:id="@+id/pickDate"
        android:text="Pick a date"/>

Our application consists of a textview and a button. The textview displays the date picked up by the user. When the application loads initially it will display the cuurent date. The button displays the Date Picker dialog to the user. Finally, add the following code to

package com.botskool.DatePicker;

import java.util.Calendar;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class DatePickerActivity extends Activity {
    /** Private members of the class */
	private TextView pDisplayDate;
    private Button pPickDate;
    private int pYear;
    private int pMonth;
    private int pDay;
    /** This integer will uniquely define the dialog to be used for displaying date picker.*/
    static final int DATE_DIALOG_ID = 0;
    /** Callback received when the user "picks" a date in the dialog */
    private DatePickerDialog.OnDateSetListener pDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    pYear = year;
                    pMonth = monthOfYear;
                    pDay = dayOfMonth;
    /** Updates the date in the TextView */
    private void updateDisplay() {
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(pMonth + 1).append("/")
                    .append(pYear).append(" "));
    /** Displays a notification when the date is updated */
    private void displayToast() {
    	Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(pDisplayDate.getText()),	 Toast.LENGTH_SHORT).show();
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        /** Capture our View elements */
        pDisplayDate = (TextView) findViewById(;
        pPickDate = (Button) findViewById(;

        /** Listener for click event of the button */
        pPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

        /** Get the current date */
        final Calendar cal = Calendar.getInstance();
        pYear = cal.get(Calendar.YEAR);
        pMonth = cal.get(Calendar.MONTH);
        pDay = cal.get(Calendar.DAY_OF_MONTH);

        /** Display the current date in the TextView */
    /** Create a new dialog for date picker */
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, 
            			pYear, pMonth, pDay);
        return null;

Here, in the beginning we have defined members of the class. In onCreate() function, we map these members to the corresponding variables in the layout. In this function, we also register to OnClickListener event of the button. When a user clicks on the button, the showDialog() function is called. A call to onCreateDialog() function is made the first time showDialog() function executes. This leads to the creation of a new dialog DatePickerDialog. After that the same dialog is used again whenever user clicks on the button for picking a date. Along with registering for on-click listener event, we retreive the current date and call updateDisplay() function inside onCreate() function.

The updateDisplay() function updates the layout's TextView, pDisplayDate, by using the values of the private date members of the class.

Additionally, the DatePickerDialog.OnDateSetListener listens for when the user has set the date (by clicking on the button). When this happens, the onDateSet() callback method is called, which is defined to update the pYear, pMonth, and pDay member fields with the new date chosen by the user. After this the private updateDisplay() method is called to update the TextView and displayToast() method is called to show a notification informing about the date change.

Thats it! Now compile and run the Android application. If you click on the button the date picker widget will be displayed as shown below:

Date Picker widget in Android

After choosing a specific date the textview gets updated and a notification is also displayed as shown below:

How to use Date Picker widget for selecting date in Android


Member since:
7 August 2017
Last activity:
6 weeks 1 day

kanye west shoes

michael kors bags

ua shoes

ray ban erika

prada handbags

payless shoes online

official kate spade website

coach outlet online

pandora rings


pandora rings uk

Coach Sunglasses Outlet

balenciaga outlet

Engagement Rings - Official

Nike Air Max 180

official michael kors

Ray Ban sunglasses

shoes sale

Nike Air Max

Air Max 180

burberry outlet

Dreamland Jewelry - Official

birkenstock sandals


Nike Air Max Couple

coach outlet online

prada outlet online

louis vuitton outlet online

Nike Air Jordan

gel kayano

official michael kors

michael kors outlet bags

Hugo Boss Store

payless shoes

Premier Jewelry - Official

ray ban outlet

Coach Handbags

pandora sale

coach watches

nike store

football jerseys

louis vuitton outlet online

coach outlet online sale

Add Your Link Free


louis vuitton us

michael kors

Nike Air Jordan Enfants

michael kors

Michael Kors Handbags Outlet Sale

kate spade outlet online

Green Cleaned

ugg boots


Nike Blazer Femmes



coach outlet

birkenstock outlet

Burberry Australia

payless near me

Chanel Outlet

north face sale

Nike Air Max

Prada Shoes - Official

michael kors outlet

ray ban wayfarer sunglasses

Oakley Sunglasses

puma sneakers outlet

payless shoes

nike free shoes

diesel watches

burberry outlet canada

prada sale

puma sneakers sale

louis vuitton outlet

Air Max 180 Homme

Jewelry Armoire - Official

coach handbags sale

Hermes Outlet

timberland shoes

Nike Air Max 2017

coachs outlet

louis vuitton outlet online

Nike Jordans

football jerseys

burberry outlet sale

Nike Free 3.0

michael kors outlet online

ray ban wayfarer

ray ban sunglasses

Rolex Watch

michael kors

michael kors

nike huarache shoes

ray ban sale


pandora necklaces

kate spade italy

coach australia

pandora uk

pandora uk

swatch watch

michael kors watches

michael kors

rayban prescription sunglasses

Brighton Jewelry - Official

prada outlet online

Nike Air Force

michael kors outlet

fulam shoes

softball jerseys

Nike Air Max

balenciaga us

Air Max 1 Homme

michael kors


Air Jordan Enfant

chrome store canada

sunglasses store

coach outlet

michael kors outlet

Nike Free Run

mk outlet online

michael kors outlet

the beat

seiko watches

michael kors


Air Max 90 Femme

hermes outlet

Nike Air Jordan enfants

hermes outlet

prada purses

kate spade outlet

nike jordan shoes

me adc8.7