---
title: "Date Picker in Android Application"  
description: "In this article, I am going to explain how to create a DatePickerDialog, which presents the date picker in a floating dialog box at the press of a but"  
author: "Chris Anderson"  
published: 2011-10-25  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/792/date-picker-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 5 minutes  

---

# Date Picker in Android Application

To provide a widget for selecting a date, use the DatePicker widget, which allows the user to select the month, day, and year, in a familiar interface.

In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370), I am going to [explain](https://www.mindstick.com/forum/159699/what-is-a-compilation-error-in-dot-net-core-explain-with-an-example) how to create a DatePickerDialog, which presents the [date picker](https://www.mindstick.com/blog/627/date-picker-using-bootstrap-in-asp-dot-net) in a [floating](https://www.mindstick.com/articles/85595/how-to-troubleshoot-when-your-floating-pond-fountain-is-not-working-properly) [dialog box](https://www.mindstick.com/blog/157/dialog-box-in-dot-net) at the press of a button. When the date is set by the user, a [TextView](https://www.mindstick.com/forum/23237/how-do-i-center-text-horizontally-and-vertically-in-a-textview-in-android) will [update](https://yourviews.mindstick.com/view/83443/g7-summit-update) with the new date.

· Start a new [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) named DatePickerDemo.

· Open the **res/layout/main.xml** file and insert the following:

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
<TextView
        android:id="@+id/dateDisplay"
        android:layout_marginTop="10dp"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello"
     />
<Button android:id="@+id/pickDate"
              android:layout_marginTop="10dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Change the date" />
</LinearLayout>
```

- Open Activity file and add the following code as shown below:

```
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerActivity extends Activity {
        private TextView mDateDisplay;
        private Button mPickDate;
        private int mYear;
        private int mMonth;
        private int mDay;
        static final int DATE_DIALOG_ID = 0;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         // capture or view elements
         mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
         mPickDate = (Button) findViewById(R.id.pickDate);
         // add a click listener on the button
         mPickDate.setOnClickListener(new OnClickListener() {
                      public void onClick(View v) {
                           showDialog(DATE_DIALOG_ID);
                     }
              });

         // get the current date
         final Calendar c = Calendar.getInstance();
         mYear = c.get(Calendar.YEAR);
         mMonth = c.get(Calendar.MONTH);
         mDay = c.get(Calendar.DAY_OF_MONTH);
         updateDisplay();
    }
     // updates the date in the TextView
     private void updateDisplay()     {
         mDateDisplay.setText(new StringBuilder().append(mMonth + 1).append("-
                             "
                            ").append(mDay).append("-").append(mYear).append(" "));
    }

     // the callback received when the user "sets" the date in the dialog
     private DatePickerDialog.OnDateSetListener mDateSetListener = new
                                                 DatePickerDialog.OnDateSetListener()
                                                DatePickerDialog.OnDateSetListener()
               {
                            public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                                   mYear = year;
                                   mMonth = monthOfYear;
                                   mDay = dayOfMonth;
                                   updateDisplay();
                           }
               };
     @Override
     protected Dialog onCreateDialog(int id)    {
         switch(id)        {
         case DATE_DIALOG_ID:
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,mDay);
        }         return null;
    }
}
```

- Run the application.

#####

##### You output looks like something this:

![Data Picker in Android Application](https://www.mindstick.com/mindstickarticle/80c1197e-c621-4f0a-b52c-120be4fd8f4d/images/2cb54df8-8043-4e65-ba6f-c2cff214f2b2.png)

When you click on the button (**Change the date**), a date picker dialog will open, you can set the date from the date picker dialog.

![Data Picker in Android Application](https://www.mindstick.com/mindstickarticle/80c1197e-c621-4f0a-b52c-120be4fd8f4d/images/ebf66a35-1d28-4947-97fb-c0a70956a94b.png)

After setting the date, a new date will display in the TextView.

---

Original Source: https://www.mindstick.com/articles/792/date-picker-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
