lokoko
lokoko

Reputation: 5803

DatePickerDialog.OnDateSetListener does not get a callback on Samsung devices

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

Upvotes: 4

Views: 7051

Answers (2)

lokoko
lokoko

Reputation: 5803

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

    });

Upvotes: 6

David
David

Reputation: 564

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

Upvotes: 2

Related Questions