0

I have a DatePickerDialog, which I call in the following way:

DatePickerDialog dialog = new DatePickerDialog(EditActivity.this, mDateSetListener, 2015, 1, 10);
dialog.setButton(
    DialogInterface.BUTTON_NEGATIVE, "Cancel",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_NEGATIVE) {
            }
        }
    });
dialog.setButton(
    DialogInterface.BUTTON_POSITIVE, "OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                ... Save values from picker here...
            }
        }
    });
dialog.show();

where the listener is:

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    }
};

The onDateSet method is called every time... on OK, on CANCEL, on DISMISS (no button pressed).

I would only like the values to be registered and stored if the user presses "OK", and thus the DialogInterface.BUTTON_POSITIVE. It works great, but is there an in-built method with pickerDialogs to get the values?

What I'm having to do right now is put a boolean inside the positive button method:

if(which == DialogInterface.BUTTON_POSITIVE){mOKClicked = true;}

and then test for it in the onDateSet:

if(mOKClicked){... save values...}

As far as I can tell, the DialogInterface.BUTTON_POSITIVE is called before the onDateSet, but I'm not sure if it is always the case. I would prefer an in-built call to gather the values set in the picker, if such a thing exists.

Thanks in advance!

Birrel
  • 4,754
  • 6
  • 38
  • 74

1 Answers1

0

but is there an in-built method with pickerDialogs to get the values?

Yes call getDatePicker() of DatePickerDialog to get selected values:

public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                ... Save values from picker here...
               DatePicker datePicker = dialog.getDatePicker();
               int selectedYear=datePicker.getYear();
               int selectedMonth=datePicker.getMonth();
               ....
            }
        }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • What about if the user is using the device with API level < 11 – Tushar Gogna Feb 11 '15 at 09:52
  • I'm getting an error, saying `Cannot resolve method 'getDatePicker()'` – Birrel Feb 11 '15 at 09:54
  • @Birrel: ok belove `API level < 11` not possible because `getDatePicker` not available – ρяσѕρєя K Feb 11 '15 at 09:56
  • @Birrel: to support all API level create custom Dialog using `DatePicker` class – ρяσѕρєя K Feb 11 '15 at 09:57
  • I had custom ones following Android's examples [here](http://developer.android.com/guide/topics/ui/controls/pickers.html), but I was unable to get a cancel button to appear, or to check if OK was pressed (so the date was being set whether the picker was dismissed or not). – Birrel Feb 11 '15 at 10:00
  • @Birrel: try to import using support library probably help – ρяσѕρєя K Feb 11 '15 at 10:06
  • @Birrel: see following example http://wptrafficanalyzer.in/blog/displaying-datepickerdialog-using-dialogfragment-in-android-with-backward-compatibilty-support-library/ – ρяσѕρєя K Feb 11 '15 at 10:18
  • @Birrel: Hi, this answer help you or not in solving issue? – ρяσѕρєя K Feb 17 '15 at 07:46
  • Not really, no. I didn't want to bring in any more libraries, and was tight on time, so I ended up setting a flag in the positive and negative buttons, which is then checked in the onDateSet function. – Birrel Feb 18 '15 at 23:56