Android - OnDateChangedListener - how do you set this?

AndroidAndroid Datepicker

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

                }
            });

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTawaniView Question on Stackoverflow
Solution 1 - AndroidChristopher OrrView Answer on Stackoverflow
Solution 2 - AndroidturbandroidView Answer on Stackoverflow
Solution 3 - Androiduser2389347View Answer on Stackoverflow
Solution 4 - AndroidAndrewView Answer on Stackoverflow
Solution 5 - AndroidCommonsWareView Answer on Stackoverflow