---
title: "Code for open date picker dialog in android."  
description: "Code for open date picker dialog in android."  
author: "Arti Mishra"  
published: 2018-08-22  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/34666/code-for-open-date-picker-dialog-in-android  
category: "android apps"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Code for open date picker dialog in android.

[Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) for open [date picker](https://www.mindstick.com/blog/627/date-picker-using-bootstrap-in-asp-dot-net) [dialog](https://www.mindstick.com/forum/562/jquery-dialog-box-not-closed-in-asp-dot-net-mvc-4)([Calendar](https://www.mindstick.com/articles/13088/how-to-choose-the-best-calendar-for-you-dynamics-crm-system)) in [android](https://www.mindstick.com/articles/13046/10-reasons-why-i-prefer-android-vs-iphone-ios) after clicking on EditText.

## Replies

### Reply by Aryan Kumar

Sure, here is the code for open date picker dialogue in Android:

Code snippet

```plaintext
public void openDatePickerDialog() {
    // Create a date picker dialog
    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    // Set the date in the text field
                    editTextDate.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
                }
            },
            Calendar.getInstance().get(Calendar.YEAR),
            Calendar.getInstance().get(Calendar.MONTH),
            Calendar.getInstance().get(Calendar.DAY_OF_MONTH));

    // Show the date picker dialog
    datePickerDialog.show();
}
```

This code will first create a `DatePickerDialog` object. The `DatePickerDialog` object will be used to display a date picker dialog to the user.

The `DatePickerDialog` object has three parameters:

- The context of the activity where the dialog will be displayed.
- A listener that will be called when the user selects a date.
- The initial date that will be displayed in the date picker dialog.

The `onDateSet()` method of the listener will be called when the user selects a date. The `onDateSet()` method has three parameters:

- The `DatePicker` object that was used to select the date.
- The year that was selected.
- The month that was selected.
- The day of the month that was selected.

In the `onDateSet()` method, you can set the date in the text field by calling the `setText()` method of the text field.

To use this code, you would need to call the `openDatePickerDialog()` method in your app. For example, you could call the method in your app's `onCreate()` method.

Here is an example of how you could call the `openDatePickerDialog()` method in your app's `onCreate()` method:

Code snippet

```plaintext
public void onCreate() {
    super.onCreate();

    // Create a text field to display the date
    editTextDate = findViewById(R.id.editTextDate);

    // Open the date picker dialog
    openDatePickerDialog();
}
```

When you run the app, you will see a date picker dialog. You can select a date in the date picker dialog and the date will be displayed in the text field.

### Reply by Arti Mishra

## /* get Instance of calendar */

Calendar myCalendar = Calendar.getInstance();

## /* Create object of EditText. */

EditText dateOfBirth = (EditText) findViewById(R.id.dob);

**/* Set value on [date](https://yourviews.mindstick.com/story/3869/propose-day-2024-exciting-date-ideas-for-you-and-your-partner) objects*/** \

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

myCalendar.set(Calendar.YEAR, year);

myCalendar.set(Calendar.MONTH, monthOfYear);

myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

updateLabel();

}

};

\

private void updateLabel() {

String myFormat = "yyyy-MM-dd"; //In which you need put here

SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

dateOfBirth.setText(sdf.format(myCalendar.getTime()));

}

\

## /* open Date Picker Dialog for dob*/

dateOfBirth.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

new DatePickerDialog(EditProfileActivity.this, date, myCalendar

.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();

}

});


---

Original Source: https://www.mindstick.com/forum/34666/code-for-open-date-picker-dialog-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
