4

For some weird reason, when i click on the positive button as part of the DatePickerDialog, the onDateSet method as part of the DateSetListener does not get invoked ONLY ON SAMSUNG DEVICES.

Here is what i am doing :

DateSetListener _datePickerDialogCallback = new DateSetListener();

DatePickerDialog _datePickerDialog = new DatePickerDialog(context, _datePickerDialogCallback, year, month, days);
_datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, StringUtil.getString(R.string.command_ok), new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface arg0, int arg1) {
      _done = true;
    }

  });

_datePickerDialog.show();



private class DateSetListener implements DatePickerDialog.OnDateSetListener {

  public void onDateSet(DatePicker view, int year, int month, int day) {

    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE),
        calendar.get(Calendar.SECOND));

    if (_done) {
      _view.setText(formatDate(calendar.getTime()));
    }
  }
}

Any suggestions on why this might be happening would be appreciated. pls. note this is only on SAMSUNG DEVICES

4
  • are you using custom date picker?
    – Hariharan
    Aug 23, 2013 at 12:24
  • I'm using a DatePickerDialog with a OnDateSetListener and it works without any problems on my Samsung Galaxy S2 (4.1.2). Which Android version are you using?
    – David
    Aug 23, 2013 at 12:34
  • check this stackoverflow.com/questions/18211684/… the first method in the link is tested on samsun galaxy s3 Aug 23, 2013 at 12:35
  • I'm using android 4.0.3
    – lokoko
    Aug 23, 2013 at 12:40

2 Answers 2

6

It looks like from ICS and above, the callback need not be defined while defining the datePickerDialog. But, the onPositiveButtonClick and onNegativeButtonClick would have to call the callback. something like :

    _datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface arg0, int arg1) {
        _done = true;
        DatePicker datePicker = _datePickerDialog.getDatePicker();
        _datePickerDialogCallback.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
      }

    });
1
  • 1
    wow, what a crappy design from Google with DatePickerDialog!! Feb 21, 2019 at 9:34
2

I'm not sure, whats wrong with your code, but this is how I did it and it works on my SG2 (I didn't do the setButton thing)

My OnDataSetListener implemented as an inner class:

class DatePickHandler implements OnDateSetListener {
    @Override
    public void onDateSet(DatePicker view, int year, 
        int monthOfYear, int dayOfMonth) {
       //do stuff
       mDateDialog.hide();
    }   
}

The creation of my DatePickerDialog in a fragment of my app

mDateDialog = new DatePickerDialog(getActivity(), 
    new DatePickHandler(), mYear, mMonth, mDay);

Than I open the Dialog inside an onClick() method of a onClickListener()

mDateDialog.show();

edit 26.08.13\

I added the following

mDateDialog.setButton(DialogInterface.BUTTON_POSITIVE, "test text", new
    DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Log.i("test", "in onclick");
});

This is how it looks now. After I press the "test text" button, my newly created onClick method gets called.

enter image description here

6
  • Hey David. tnks so much for your response. Does it work with the setButton with a OnClickListener as part of that ?
    – lokoko
    Aug 26, 2013 at 9:53
  • I added it to my answer above. Short version: Yes, works without any problems ;)
    – David
    Aug 26, 2013 at 14:54
  • The onClick() getting called is fine but does your OnDateSetListener callback get invoked ?
    – lokoko
    Aug 27, 2013 at 8:20
  • The onDataSet() method? Yes, that one too.
    – David
    Aug 27, 2013 at 9:03
  • Possible to share your activity code ? that this datePickerDialog does.
    – lokoko
    Aug 27, 2013 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.