Set Limit on the DatePickerDialog in Android? -


how limit on datepickerdialog choose date 2-12 years in past , 0-2 year current date . have datepickerdialog in activity used getting date of birth child , infant .child age (2-12yrs)and infant age can (0-2ys). functionality in calender.if pass 1 age limit (2-12yrs) , pass 2 age limit (0-2yrs).please functionality in calender.

my calender code :

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 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 = calendar.getinstance();      switch (id) {     case date_dialog_id:         // set date picker 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; } 

all other answers seem rather convoluted, i'm going state obvious: can underlying datepicker datepickerdialog (by calling getdatepicker()) , set bounds using:

where argument usual number of milliseconds since january 1, 1970 00:00:00 in default time zone. you'll still have calculate these values of course, should trivial calendar class: take current date , add or substract x years.

the more attentive reader notice aforementioned setters weren't available until api level 11. if you're targeting (or newer) platform only, you're go. if want support i.e. gingerbread devices (android 2.3 / api level 9+), can use backported version of datepicker in stead.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -