Android datepicker min max date before api level 11

You can set range with init datePicker method.
Example with min value :

// Calendar
this.calendar = new GregorianCalendar();
this.datePicker = (DatePicker) findViewById(R.id.xxxxx);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // (picker is a DatePicker)
    this.datePicker.setMinDate(this.calendar.getTimeInMillis());
} else {
    final int minYear = this.calendar.get(Calendar.YEAR);
    final int minMonth = this.calendar.get(Calendar.MONTH);
    final int minDay = this.calendar.get(Calendar.DAY_OF_MONTH);

    this.datePicker.init(minYear, minMonth, minDay,
            new OnDateChangedListener() {

                public void onDateChanged(DatePicker view, int year,
                        int month, int day) {
                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, month, day);

                    if (calendar.after(newDate)) {
                        view.init(minYear, minMonth, minDay, this);
                    }
                }
            });
    Log.w(TAG, "API Level < 11 so not restricting date range...");
}

Leave a Comment