I have implemented a DatePicker
dialog using the following code:
static final int DATE_PICKER_FROM = 0;
DatePickerDialog.OnDateSetListener from_dateListener;
private EditText editDateStart;
...
private void addListenersToButtons(){
editDateStart = (EditText)findViewById(R.id.editDateStart);
editDateStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_PICKER_FROM);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_PICKER_FROM:
return new DatePickerDialog(this, from_dateListener,2,10,2013);
}
return null;
}
The following is the inizialization of the listener:
from_dateListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
int year = selectedYear;
int month = selectedMonth;
int day = selectedDay;
editDateStart.setText(
new StringBuilder()
.append(pad(day)).append("/")
.append(pad(month + 1)).append("/")
.append(year).append(" "));
}
};
When the EditText field is clicked the Dialog showed is the following:
So, the date I tried to set using the code:
DatePickerDialog(this, from_dateListener,2,10,2013);
does not appears. As you can see the date is 1 gen 1900.
Another problem occurs when I try to change the day in the datepicker. For example I have setted from 1 to 2 and the result is the following:
The calendar has reached the date 2 feb 2036. Why?
NOTE: If I change the year in the datepicker from 1900 to 1901 the calendar year change to 2037, but If I set the date to 1902 in the datepicker, the year in calendar become 1902 (and so on)