PeterB
PeterB

Reputation: 109

How to reset DatePicker when selecting cancel Button

I have a datePicker dialog in my app. It works fine when I select a date. However if in the dialog I change the date and then press cancel, the original edittext remains unchanged as it should, but the Date picker still has the date from before when it was cancelled. I would like to make sure that EVERY time I go into the Date Picker, it sets the date from the EditText. My code is as follows.

onCreate() Method

//  Date Listener
    fdEtDate.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            showDialog(DATE_DIALOG_ID);
            return false;
        }
    });

Further down the Activity

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    dateSetListener,
                    cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
    case STD_DIALOG_ID:
        return new TimePickerDialog(this,
                    stdSetListener,
                    stdHH, stdMM,
                    true);
    case STA_DIALOG_ID:
        return new TimePickerDialog(this,
                    staSetListener,
                    staHH, staMM,
                    true);
    }
    return null;
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener dateSetListener =
    new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DATE, dayOfMonth);
            updateDisplay();
        }
};
// updates the date in the TextView
private void updateDisplay() {
    fdEtDate.setText(
        new StringBuilder()
                .append(Utils.pad(cal.get(Calendar.DATE))).append(Const.SQL_DATE_SEP)
                .append(Utils.pad(cal.get(Calendar.MONTH)+1)).append(Const.SQL_DATE_SEP)
                .append(cal.get(Calendar.YEAR)));

    fdEtStd.setText(
        new StringBuilder()
                .append(Utils.pad(stdHH)).append(Const.SQL_TIME_SEP)
                .append(Utils.pad(stdMM)));

    fdEtSta.setText(
        new StringBuilder()
                .append(Utils.pad(staHH)).append(Const.SQL_TIME_SEP)
                .append(Utils.pad(staMM)));
}

Is seems to create a new Dialog each time, so why isn't it initialised each time with the Date from fdEtDate (EditText)

Thanks in advance

Upvotes: 5

Views: 6494

Answers (4)

Kushal
Kushal

Reputation: 8478

To reset date picker on cancel click :

  1. Add onDismissListner on date picker
  2. Inside onDismissListener, call updateData method

Example :

datePicker.setOnDismissListener((dialog) -> {
    datePicker.updateData(Year, Month, Date);
}

Note : You may be thinking that is user clicks Ok this code will give problem. Ideally No. As you are calling updateData in onDateSet then you won't get any problem.

Upvotes: 0

Leandro Teodoro
Leandro Teodoro

Reputation: 31

This is simple.

You could use the following method to reset the calendar

public void resetDate(myDatePicker){

       Calendar now = Calendar.getInstance();
       this.myDatePicker.updateDate(now.get(now.YEAR), now.get(now.MONTH), now.get(now.DAY_OF_MONTH));
    }

This way it is very easy and works perfectly.

Upvotes: 3

Chiara
Chiara

Reputation: 1887

you should use onPrepareDialog to set the initial date every time the dialog is shown.

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
    case START_DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(
            mCalendar.get(Calendar.YEAR),
            mCalendar.get(Calendar.MONTH),
            mCalendar.get(Calendar.DAY_OF_MONTH))
    }
}

A dialog is created only the first time it is shown and from that point it is reused unless you destroy it explicitly with removeDialog().

If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it

http://developer.android.com/guide/topics/ui/dialogs.html

Upvotes: 8

kaspermoerch
kaspermoerch

Reputation: 16570

It's probably because you change the values of your Calendar object in onDateSet. Try to verify when the method is call by adding some output to LogCat e.g. Log.e( "MyTag", "onDateSet was called!" );. Then experiment a little and see when its called.

If this is the case and onDateSet is called when you didn't expect it, you should change the time at which you set the calendar.

Upvotes: 0

Related Questions