I have implemented a Date picker as follows:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
String strMonth = "";
if (month == 0) {
strMonth = "January";
} else if (month == 1) {
strMonth = "February";
} else if (month == 2) {
strMonth = "March";
} else if (month == 3) {
strMonth = "April";
} else if (month == 4) {
strMonth = "May";
} else if (month == 5) {
strMonth = "June";
} else if (month == 6) {
strMonth = "July";
} else if (month == 7) {
strMonth = "August";
} else if (month == 8) {
strMonth = "September";
} else if (month == 9) {
strMonth = "October";
} else if (month == 10) {
strMonth = "November";
} else if (month == 11) {
strMonth = "December";
}
day = selectedDay;
tvPurchaseDate.setText(new StringBuilder().append(day).append(" ").append(strMonth).append(" ").append(year).append(" "));
tvPurchaseDate.setTextColor(Color.BLACK);
}
};
The problem that I am having is that the date picker displays the date wrong as shown in the images below.
As the dialog is created:
If I select a different date:
Note the date is not the current date (today) and the year displayed on the wheel and the year displayed at the top of the datepicker are completely different. Am I doing anything wrong? I do not understand, because I have used this code before and it worked fine. The date picker is called from within a view flipper. Thank you in advance
EDIT
This is how I call the Date picker:
private int day, month, year;
In my onClick:
showDialog(DATE_DIALOG_ID);
Then the onCreateDialog() is called (displayed in the code above), all this code is inside the activity that the call is made from.