views:

367

answers:

2

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.

+2  A: 

Once you've created your DatePicker, you need to initialise 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).

Christopher
The Android API is really something. I wonder who came up with this ridiculous approach
Tawani
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
+1  A: 

Call init() on the DatePicker object.

CommonsWare