No need to add new
. That way you would be creating a class with the interface DatePicker.OnDateChangedListener
.
The correct thing is for you to capture through findViewById
or similar and add that event in the method setOnDateChangedListener
.
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 less than 25, use the method init
:
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) );
}
});
Just use Datepicker.Ondatechangedlistener
– Valdeir Psr
Does not work, shows as error
– Woton Sampaio
What error appears?
– Valdeir Psr
@Valdeirpsr I’ll attach some prints, it looks better
– Woton Sampaio
@Valdeirpsr ready, added the prints
– Woton Sampaio