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

Charles
  • 50,943
  • 13
  • 104
  • 142
lokoko
  • 5,785
  • 5
  • 35
  • 68
  • are you using custom date picker? – Hariharan Aug 23 '13 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 '13 at 12:34
  • check this http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment/18212061#18212061 the first method in the link is tested on samsun galaxy s3 – Raghunandan Aug 23 '13 at 12:35
  • I'm using android 4.0.3 – lokoko Aug 23 '13 at 12:40

2 Answers2

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());
      }

    });
lokoko
  • 5,785
  • 5
  • 35
  • 68
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

David
  • 554
  • 7
  • 9
  • Hey David. tnks so much for your response. Does it work with the setButton with a OnClickListener as part of that ? – lokoko Aug 26 '13 at 09:53
  • I added it to my answer above. Short version: Yes, works without any problems ;) – David Aug 26 '13 at 14:54
  • The onClick() getting called is fine but does your OnDateSetListener callback get invoked ? – lokoko Aug 27 '13 at 08:20
  • The onDataSet() method? Yes, that one too. – David Aug 27 '13 at 09:03
  • Possible to share your activity code ? that this datePickerDialog does. – lokoko Aug 27 '13 at 09:39
  • The DateDialog gets opened in a Fragment. The Fragment is hosted by an almost empty activity. I'm using ActionbarSherlock because there is also a navigation drawer in a different activity. Do you use a fragment or an activity? Whats in in your context variable? – David Aug 27 '13 at 10:18