Set Limit on the DatePickerDialog in Android?

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;
}

Solution 1:

All the other answers seem rather convoluted, so I'm just going to state the obvious: you can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using:

  • setMinDate(long minDate)
  • setMaxDate(long maxDate)

Where the argument is the usual number of milliseconds since January 1, 1970 00:00:00 in the default time zone. You'll still have to calculate these values of course, but that should be trivial to do with the Calendar class: just take the current date and add or substract x years.

The more attentive reader will notice that the aforementioned setters weren't available until API level 11. If you're targeting that (or newer) platform only, then you're good to go. If you also want to support i.e. Gingerbread devices (Android 2.3 / API level 9+), you can use a backported version of DatePicker in stead.

Solution 2:

Setting minumum and maximum date for the DatePickerDialog is not enough. User can click on the year and scroll in between years.

You should also set year range on the DatePicker like this:

datePicker.setYearRange(calendar.get(Calendar.YEAR), calendar.get(Calendar.YEAR) + 1);

Solution 3:

I am not in a position to give you the working code, since I am on a tour and didn't have access to my PC. but I can give you a way to achieve your aim (or so i think).

I am not sure this will work perfectly, but any way worth a try.

Pls try this code and inform me.

"DatepickerType" is an Integer public Member variable of the class.

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) {
        int y = Calendar.getInstance().get(Calendar.YEAR);
        switch(DatepickerType){
        case 1:
            if(selectedYear>(y-2)){
                //Add message if you want
                selectedYear = y-2;
            }
                else if(selectedYear<(y-12)) {
                //Add msg if u want
                    selectedYear = y-12;
            }

        case 2:
            if(selectedYear>(y)){
                //Add message if you want
                selectedYear = y;
            }
                else if(selectedYear<(y-2)) {
                //Add msg if u want
                    selectedYear = y-2;
            }
        }

        String year1 = String.valueOf(selectedYear);
        String month1 = String.valueOf(selectedMonth+1);
        String day1 = String.valueOf(selectedDay);
        //do what you need
        setLastStatus();

    }
};