63

There is an event listener in Android called DatePicker.OnDateChangedListener. I am trying to set a DatePicker view's on date changed listener as follows:

DatePicker dp = new DatePicker(getContext());
dp.setOnDateChangedListener(this); 
//where this is my activity extends DatePicker.OnDateChangedListener

But guess what? Date picker does not have a method called setOnDateChangedListener.

My question is:

  1. How then do you set a date changed listener in Android?
  2. If it is not possible to set a date changed listener, what is the purpose for this event?

Any documentation/tutorials will be very helpful.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Tawani
  • 11,067
  • 20
  • 82
  • 106

5 Answers5

122

Once you've created your DatePicker, you need to initialize it with the date you want to display at first. That's the point at which you can add your listener.

See DatePicker.init(int, int, int, OnDateChangedListener).

Update

26 API allows to set listener: DatePicker.setOnDateChangedListener()

user924
  • 8,146
  • 7
  • 57
  • 139
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • 121
    The Android API is really something. I wonder who came up with this ridiculous approach – Tawani Jan 28 '10 at 14:36
  • 1
    It doesn't seem that ridiculous to me, and at least it's in the API docs. Anyway, I imagine the reason is because Android widgets usually have several constructors, for when you wish to control what style and layout attributes it should be created with -- so they've kept to that convention rather than creating numerous constructors with many parameters. – Christopher Orr Jan 28 '10 at 17:09
  • 5
    It does seem appropriate to have a setOnDateChangedListener() method like is present to set a click listener. I wonder why they make you initialize it to a date when it auto initializes to today for you? – Ross Hambrick Aug 02 '11 at 18:00
  • 1
    @ChristopherOrr the terrible part here is mainly that there is a TimePicker and a DatePicker, which have completely different interfaces. (Wow, old post.... nm) – dstibbe Oct 29 '15 at 21:46
  • 1
    Wow, I've just spent 2hours to find out how to set a simple listener to a DatePicker until I found that its not working. Note: only from API level 26 you can directly use setOnDateChangedListener(). If you are below you have to use a workaround -.- – ice_chrysler Jun 23 '20 at 12:23
47

Best way is

        DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
                    Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);

                }
            });
turbandroid
  • 2,296
  • 22
  • 30
10

This view is in fact a combination of four views, and they are :

Three Spinners

One CalendarView

As of the OnDateChangeListener, the object you passed in to the init method will be simply passed to the contained CalendarView, and I believe that you know that there is a setOnDateChangeListener method in the good old CalendarView...... ......

In the DatePicker class, there is a method called the getCalendarView, and it is the method you can call if you want to get your hands on the contained CalendarView.

Once you get your hands on the contained CalendarView, then, needlessly to say, you can call its setOnDateChangeListener

user2389347
  • 101
  • 1
  • 2
  • 3
    This is a great workaround for a terrible API. Thanks for this - you just saved my bacon! – head in the codes Feb 06 '14 at 18:57
  • datePicker.getCalendarView().setOnDateChangeListener() totally works! Thanks! Not very intuitive though. Still think the API should have been datePicker.setOnDateChangeListner(). – ONE Jul 02 '17 at 07:56
9

Something like this:

DatePicker myDatePicker = (DatePicker) findViewById(R.id.my_date_picker);
myDatePicker.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
        Log.d("tag", "finally found the listener, the date is: year " + year + ", month "  + month + ", dayOfMonth " + dayOfMonth);
    }
});
Andrew
  • 3,545
  • 4
  • 31
  • 37
5

Call init() on the DatePicker object.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491