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...");
}
This DatePicker custom class Allow us Specify Max and Min Date
public class DatePickerDialogWithMaxMinRange extends DatePickerDialog {
static int maxYear=2005;
static int maxMonth=11;
static int maxDay=31;
int minYear=1955;
int minMonth=0;
int minDay=1;
public DatePickerDialogWithMaxMinRange(Context context, OnDateSetListener callBack,int minYear,int minMonth,int minDay,int maxYear,int maxMonth,int maxDay) {
super(context,callBack, maxYear, maxMonth, maxDay);
this.minDay = minDay;
this.minMonth = minMonth;
this.minYear = minYear;
DatePickerDialogWithMaxMinRange.maxDay = maxDay;
DatePickerDialogWithMaxMinRange.maxMonth = maxMonth;
DatePickerDialogWithMaxMinRange.maxYear = maxYear;
}
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
super.onDateChanged(view, year, monthOfYear, dayOfMonth);
if (year > maxYear ||monthOfYear > maxMonth && year == maxYear||
dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth){
view.updateDate(maxYear, maxMonth, maxDay);
}else if(year < minYear ||monthOfYear < minMonth && year == minYear||
dayOfMonth < minDay && year == minYear && monthOfYear == minMonth){
view.updateDate(minYear, minMonth, minDay );
}
}
}
// use this code in your Activity class or Fragment specify member as
private int intCurrentYear;
private int intCurrentMonth;
private int intCurrentDay;
private int intMaxYear;
private int intMaxMonth;
private int intMaxDay;
private int intMinYear;
private int intMinDay;
private int intMinMonth;
DatePickerDialogWithMaxMinRange datePickerDialog= null;
DatePickerDialog.OnDateSetListener datePickerOnDateSetListener;
Calendar myCalendar;
// call this method in onCreate or where ever u want to..
note: you need pass the listener custom datepickerdialog to update the date after select in datepicker dialog
public void setDate() {
/*
* Initialise Listener for date set
*/
datePickerOnDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
edtTxtPlayerBirthDay.setText(new StringBuilder().append(year)
.append("-").append(monthOfYear + 1).append("-")
.append(dayOfMonth));
}
};
// initialise DatePicker
myCalendar = Calendar.getInstance();
intCurrentYear = myCalendar.get(Calendar.YEAR);
intCurrentMonth = myCalendar.get(Calendar.MONTH);
intCurrentDay = myCalendar.get(Calendar.DAY_OF_MONTH);
intMaxYear = intCurrentYear - 2000;
intMaxMonth = intCurrentMonth;
intMaxDay = intCurrentDay;
intMinYear = intCurrentYear - 1950;
intMinMonth = intCurrentMonth;
intMinDay = intCurrentDay;
datePickerDialog = new DatePickerDialogWithMaxMinRange(
context, datePickerOnDateSetListener,intMinYear,intMinMonth,intMinDay,intMaxYear,intMaxMonth,intMaxDay);
}
when creating date picker pass the mindate and maxdate as per u r requirement for showing datepicker when button clicked or any other control put this code inside for that onclicklistener...
datePickerDialog.show();
Found a cool way to set the min
and max
date for DatePickerDialog
. Works for All Android versions. Simply you need to return the DatePickerDialog
based on Android version
protected Dialog onCreateDialog(int id)
{
switch ( id )
{
case DATE_DIALOG_ID:
DatePickerDialog dialog = null;
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
{
dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
{
mTextView.setText(selectedDate + "/" + selectedMonth + 1 + "/" + selectedYear);
}
}, year, month - 1, date);
Calendar currentDate = Calendar.getInstance();
dialog.getDatePicker().setMaxDate(currentDate.getTimeInMillis());
//If you need you can set min date too
}
else
{
dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
{
mTextView.setText(selectedDate + "/" + selectedMonth + 1 + "/" + selectedYear);
}
}, year, month - 1, date)
{
@Override
public void onDateChanged(DatePicker view, int year, int month, int day)
{
if ( year <= maxYear && month + 1 <= maxMonth && date <= maxDate ) //meets your criteria
{
view.updateDate(year, month, date); //update the date picker to selected date
}
else
{
view.updateDate(maxYear, maxMonth - 1, maxDate); // or you update the date picker to previously selected date
}
}
};
}
return dialog;
}
return null;
}
Edit your DatePicker.java adding the following 3 code excerpts.
Declare the variables used for limiting the minimum and maximum date.
// Variables for defining minimum date
private int minimumDay;
private int minimumMonth;
private int minimumYear;
// Variables for defining maximum date
private int maximumDay;
private int maximumMonth;
private int maximumYear;
After the constructors, your have to override onDateChanged method.
// Called every time the user changes DatePicker values
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Test if chosen date is before minimum date
boolean beforeMinDate = false;
boolean afterMaxDate = false;
if(year < minimumYear){
beforeMinDate = true;
}
else if(year == minimumYear){
if(monthOfYear < minimumMonth){
beforeMinDate = true;
}
else if(monthOfYear == minimumMonth){
if(dayOfMonth < minimumDay){
beforeMinDate = true;
}
}
}
// Test if chosen date is after maximum date
if(!beforeMinDate){
if(year > maxYear){
afterMaxDate = true;
}
else if(year == maxYear){
if(monthOfYear > maxMonth){
afterMaxDate = true;
}
else if(monthOfYear == maxMonth){
if(dayOfMonth > maxDay){
afterMaxDate = true;
}
}
}
}
// If chosen date is before minimum date, update the date and internal
// calendar to minimum date, else, check similarly fot the maximum
// date, else, use the valid chosen date.
if(beforeMinDate)
{
mCalendar.set(minimumYear, minimumMonth, minimumDay,
mCalendar.get(Calendar.HOUR_OF_DAY),
mCalendar.get(Calendar.MINUTE));
updateDate(minimumYear, minimumMonth, minimumDay);
}
else if (afterMaxDate)
{
mCalendar.set(maximumYear, maximumMonth, maximumDay,
mCalendar.get(Calendar.HOUR_OF_DAY),
mCalendar.get(Calendar.MINUTE));
updateDate(maximumYear, maximumMonth, maximumDay);
}
else
mCalendar.set(year, monthOfYear, dayOfMonth,
mCalendar.get(Calendar.HOUR_OF_DAY),
mCalendar.get(Calendar.MINUTE));
}
And now, create setter methods for the minimum and maximum dates.
// Method to define minimum permitted date for the picker.
public void setMinimumDate(int minimumDay, int minimumMonth,
int minimumYear)
{
this.minimumDay = minimumDay;
this.minimumMonth = minimumMonth;
this.minimumYear = minimumYear;
}
//Method to define maximum permitted date for the picker.
public void setMaximumDate(int maximumDay, int maximumMonth,
int maximumYear)
{
this.maximumDay = maximumDay;
this.maximumMonth = maximumMonth;
this.maximumYear = maximumYear;
}
Second part, in the activity you call the DatePicker dialog, you need to set the minimum and maximum desired dates. The code below sets the current date as minimum and two years in the future as the maximum date.
// Define current date as the minimum desired date
Calendar c = Calendar.getInstance();
int currentDay = c.get(Calendar.DAY_OF_MONTH);
int currentMonth = c.get(Calendar.MONTH);
int currentYear = c.get(Calendar.YEAR);
inicioDateTimePicker.setMinimumDate(currentDay, currentMonth,
currentYear);
// Define two years in the future as the maximum desired date
Calendar c = Calendar.getInstance();
int currentDay = c.get(Calendar.DAY_OF_MONTH);
int currentMonth = c.get(Calendar.MONTH);
int currentYear = c.get(Calendar.YEAR);
inicioDateTimePicker.setMinimumDate(currentDay, currentMonth,
currentYear + 2);