DatePickerDialog in Android always shows the previously set date

609 Views Asked by At

I am doing the following in my app:

1.I have two EditText boxes.

  1. On clicking each of them a DatePickerDialog pops up.

  2. I set the date in the Datepicker.

  3. On closing the dialog box the date gets set in the edittext.

The problem is as follows.

After setting the date in the datepicker , the datepicker always shows the date I have set and not the current date. i.e. if I set the date as 2014-09-09 when I click the edittext again the date on the datepicker is shown as the above set date and not the present date.

Can anyone guide me how to fix this step by step..

My codes are as follows:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.edt_date_from:
        InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edt_date_to.getWindowToken(), 0);

        edt = 1;
        showDialog(DATE_DIALOG_ID);
        break;
    case R.id.edt_date_to:
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        edt = 2;

        showDialog(DATE_DIALOG_ID);
        break;
}



   @Override
protected Dialog onCreateDialog(int id) {

    switch (id) {

    case DATE_DIALOG_ID:

        Calendar cal = Calendar.getInstance();

        int iDay,
        iMonth,
        iYear;

        iDay = cal.get(Calendar.DAY_OF_MONTH);
        iMonth = cal.get(Calendar.MONTH);
        iYear = cal.get(Calendar.YEAR);
        DatePickerDialog dp = new DatePickerDialog(this, pDateSetListener,
                iYear, iMonth, iDay);
        dp.updateDate(iYear, iMonth, iDay);

        return dp;

    }

    return null;

}




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

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        years = year;
        month = monthOfYear;
        day = dayOfMonth;

        updateDisplay();

    }

};

public void updateDisplay() {
    // TODO Auto-generated method stub
    if (edt == 1) {
        date_frm.setText(new StringBuilder()

        .append(day).append("-").append(month + 1).append("-")
                .append(years).append(" "));
    } else {
        edt_date_to.setText(new StringBuilder()

        .append(day).append("-").append(month + 1).append("-")
                .append(years).append(" "));
    }
}
2

There are 2 best solutions below

0
On

From the docs:

A call to onCreateDialog(int, Bundle) will be made with the same id the first time this is called for a given id. From thereafter, the dialog will be automatically saved and restored.

If you insist on using these deprecated activity-managed dialogs and not DialogFragments, move the init code that resets the date to onPrepareDialog().

2
On

You can override the onCreateDialog() method in your activity

just add a onClick listener to your edit text and in the onClick method have this code

showDialog(999);

Then

@Override
protected Dialog onCreateDialog(int id) {
    if (id == 999) {
        Date date = new Date();
        int year=date.getYear();
        int month=date.getMonth();
        int day=date.getDay();
        return new DatePickerDialog(this, myDateListener, year, month, day);
    }
    return null;
}

it will do the job...