How to limit on DatePickerDialog to choose a date from 2-12 years in the past or from 0-2 years from the current date?
I have DatePickerDialog in activity which is used for getting the date of birth for child and infant. Child age will be (2-12yrs) and infant age can be (0-2ys). If I pass 1 then the age limit should be (2-12yrs) and if I pass 2 the age limit should be (0-2yrs).
What could I do to get this functionality in my calendar?
My Calender code is:
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "dd/MM/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dateofBirth.setText(sdf.format(myCalendar.getTime()));
}
};
dateofBirth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
public Dialog onCreateDialog(int id) {
final Calendar now = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, date,myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)){
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){
if (year > now.get(Calendar.YEAR))
view.updateDate(myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
if (monthOfYear > now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR))
view.updateDate(myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
if (dayOfMonth > now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) &&
monthOfYear == now.get(Calendar.MONTH))
view.updateDate(myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
}
};
return _date;
}
return null;
}