No need to add new
. That way you would be creating a class with the interface DatePicker.OnDateChangedListener
.
The correct way is to capture through findViewById
or similar and add this event to setOnDateChangedListener
method.
Example:
DatePicker datePicker = findViewById(R.id.datePicker);
datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
System.out.println( String.format(Locale.CANADA, "%d/%d/%d", dayOfMonth, monthOfYear, year) );
}
});
In versions under 25, use the init
method:
Example:
DatePicker datePicker = findViewById(R.id.datePicker);
Calendar calendar = Calendar.getInstance();
datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
System.out.println( String.format(Locale.CANADA, "%d/%d/%d", dayOfMonth, monthOfYear, year) );
}
});