Developer Developer - 1 year ago 76
Android Question

Open a DatePickerDialog on Click of EditText takes two clicks

I want to open a Calendar on the click of Edit text. After that I want to set the date which the user selects from the Calendar in the edit text. Problem is that, only when I click on the EditText for the second time then the calendar open. Please help me to resolve the issue(

why calendar don't open for the first time

EditText XML code

android:hint="dd/mm/yyyy" />

Activity Code

public void informationPopUp() {
final Dialog dialog= new Dialog(MainActivity.this,;
dateofBirth = (EditText)dialog.findViewById(;;
myCalendar = Calendar.getInstance();

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

public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);


dateofBirth.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

protected Dialog onCreateDialog(int id) {
final Calendar now = Calendar.getInstance();

switch (id) {
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, date,myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),

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

if (year > now.get(Calendar.YEAR))

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

if (monthOfYear > now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR))
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

if (dayOfMonth > now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) &&
monthOfYear == now.get(Calendar.MONTH))
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
return _date;
return null;

I am not understanding what I have done wrong. Please suggest a solution.

Answer Source

I'll try to address your problem, but I am not completely sure about the first reason.

  1. The calendar opening only on the second click is because you are using an edittext. On the first click, your Edit Text will get focus. then the second click only calls the onClickListener.

    If you are not looking forward to edit the date set manually (using keyboard), then why not using a TextView to display the selected Date?

  2. The problem with the date not updating in editText is occurring because you are not setting the DateSetListener in your code. You need to set that to notify the system that a Date was set. The DateChange listener only returns the date while you are changing the date, and it doesn't appear that you are setting the date in the EditText.

Try this code:

            Calendar cal = Calendar.getInstance(TimeZone.getDefault());
            DatePickerDialog datePicker = new DatePickerDialog(this,

            datePicker.setTitle("Select the date");

            return datePicker;
    } catch (Exception e) {
            "An error occured while showing Date Picker\n\n"
            + " Error Details:\n" + e.toString(), "OK");
    return null;

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
        String year1 = String.valueOf(selectedYear);
        String month1 = String.valueOf(selectedMonth + 1);
        String day1 = String.valueOf(selectedDay);
        TextView tvDt = (TextView) findViewById(;
        tvDt.setText(day1 + "/" + month1 + "/" + year1);

In this, I am updating the date to a TextView with the ID "tvDate". I advise using a TextView instead of EditText and try this code.


If you need to use EditText and load the calender in the first click, then try setting an onFocusListner to the editText instead of onClickListner.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
           // Show your calender here 
        } else {
           // Hide your calender here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download