Android - OnDateChangedListener - how do you set this?

Android has a DatePicker.OnDateChangedListener event listener . I am trying to set the DatePicker view to a modified date 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 setOnDateChangedListener method.

My question is:

  • How do you set the listening date in Android?
  • If it is not possible to set a modified date listener, what is the purpose of this event?

Any documentation / tutorials will be very helpful.

+53
android android-datepicker
Jan 12 '10 at 18:00
source share
5 answers

After creating the DatePicker you need to initialize it with the date that you want to display at the beginning. This is where you can add a listener .

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

+100
Jan 12 '10 at 18:11
source share

The best way -

  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); } }); 
+34
Jan 22 '16 at 17:20
source share

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

Three spinning rods

One CalendarView

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

There is a method in the DatePicker class called getCalendarView, and it is a method that you can call if you want to access the contained CalendarView.

Once you access the contained CalendarView, you can of course call it setOnDateChangeListener

+11
May 16 '13 at 9:38
source share

Something like that:

 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); } }); 
+8
Aug 09 '15 at 18:51
source share
+3
Jan 12 '10 at 18:11
source share



All Articles