16

I'm working with a DatePicker and finding that under Android 5.0 it will not call the OnDateChanged() method in its OnDateChangedListener when it's in CalendarView mode even though a new date has been selected. If android:datePickerMode="spinner" is set in the DatePicker's xml tag, the DatePicker will appear as spinners and it will call OnDateChanged() when a new date is selected. In earlier versions of Android, a DatePicker calls OnDateChanged() when a new date is selected in both CalendarView and Spinners versions. Here's the relevant code:

@SuppressLint("InflateParams")
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);

 DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_DatePicker);
 datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
       @Override
       public void onDateChanged(DatePicker view, int year, int month, int day) {
           //Translate year, month, day into a Date object using a calendar
           mDate = new GregorianCalendar(year, month, day).getTime();
           //Update argument to preserve selected value on rotation
           getArguments().putSerializable(EXTRA_DATE, mDate);
       }
  });

In my application, onDateChanged() doesn't get called and mDate doesn't get changed if the DatePicker is in CalendarView mode under Lollipop, but OnDateChanged() does get called and mDate does change if the DatePicker is in Spinners mode. Under earlier versions of Android, OnDateChanged() gets called and mDate gets changed in both versions of the DatePicker.

Is there any way to get a CalendarView DatePicker in 5.0 to call OnDateChanged()? Failing that, how else can I retrieve a changed date from the DatePicker when it's in CalendarView mode?

knutsondc
  • 515
  • 4
  • 14
  • 3
    This is a framework bug that's been fixed for the next release. I don't think there is a clean way to work around it, unfortunately. – alanv Dec 10 '14 at 19:36
  • @alanv I was afraid it was going to be something like that 8^( Thanks for your comment, though. Now I can stop beating my head against the wall about this. – knutsondc Dec 10 '14 at 19:48
  • 2
    The best you can do for now might be to just set the date picker mode to "spinner" from XML. – alanv Dec 10 '14 at 20:04
  • @alanv That's the route I've decided to take for now. Thanks again. – knutsondc Dec 10 '14 at 20:13
  • 1
    Putting the AOSP ticket here for future reference: https://code.google.com/p/android/issues/detail?id=147657 – eski Sep 01 '15 at 18:37

2 Answers2

7

I face the same issue and the thing is onDateChange() and onTimeSet() listeners for DatePicker and TimePicker is not called in Nexus devices with lollipop Update.

The reason is in nexus devices, since the clock app is updated, the listeners are not working.

The work around is once the dialog dismiss, you need to create you own listener and set the values in a calendar object using the datepicker get() methods and pass the calendar to the listener.

A simple sample code is

/**
 * Returns the calendar instance once the date and time is set.
 * @return
 */
public Calendar getDateTime() {
    mCalendar.set(datePicker.getYear(),
                  datePicker.getMonth(),
                  datePicker.getDayOfMonth(),
                  timePicker.getCurrentHour(),
                  timePicker.getCurrentMinute());
    return mCalendar;
}
Remc4
  • 1,044
  • 2
  • 12
  • 24
Vishnu Prabhu
  • 449
  • 1
  • 5
  • 19
0

Set this in your Xml-Layout:

<DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:datePickerMode="spinner"
        android:spinnersShown="false"
        android:id="@+id/datepicker_popupwindow"/>

With "android:spinnersShown="false" " you tell it to not show the Spinner. But it will call the 'onDateChanged' Method.

Lukay
  • 25
  • 5