2

I am stuck at point where I need to ask for birthdate from user as input. I need to put restriction that user should not be able to add any date before 10 year).

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Yesha Shah
  • 408
  • 1
  • 5
  • 17
  • 2
    check this answer http://stackoverflow.com/a/18353944/4235666 – jorgeavilae Apr 15 '17 at 10:13
  • 1
    Possible duplicate of [Set Limit on the DatePickerDialog in Android?](http://stackoverflow.com/questions/18272306/set-limit-on-the-datepickerdialog-in-android) – GKFX Apr 15 '17 at 10:14

3 Answers3

3

I think you mean users can't add any date earlier than 10 year before right? Or your users are mostly kids aged 10 to 0?!

Since your limit date is based on current date, you have to set limit programmatically using setMinDate(long date) and setMaxDate(long date). As you can see those method works with date in millisecond so you have to get dare in millis first:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -10);                //Goes 10 Year Back in time ^^
long upperLimit = calendar.getTimeInMillis();    //Get date in millisecond (epoch)

, and then set the limit using above method:

datePicker.setMaxDate(upperLimit);
Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
  • Actually I need to take birthdate, and want to set limit that user should be able to select any date before 10 years than present date. – Yesha Shah Apr 15 '17 at 11:17
  • Oh, got it! You can easily set any lower and/or upper limit using the same method explained above.. Let me know if you have any problem.. – Keivan Esbati Apr 15 '17 at 11:28
0

You could do this:

DatePicker datePicker = (DatePicker) findViewById(R.id.event_date);
datePicker.setMinDate(dateTenYearsAgo);

More info: https://stackoverflow.com/a/18353944/4235666

Community
  • 1
  • 1
jorgeavilae
  • 208
  • 1
  • 9
0

try with this code in datePicker dialog:

    Calendar c = Calendar.getInstance();
    c.add(Calendar.YEAR, -10);
    long tenYearBack = c.getTimeInMillis();
    datePickerDialog.getDatePicker().setMinDate(tenYearBack);
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30