Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android datepicker min max date before api level 11

I am trying to set the min and max date of the date picker in Android to before API level 11. I used the following code:

mDatePickerField = startDatePickerDialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);

DatePicker startDatePickerInstance =(DatePicker)mDatePickerField.get(startDatePickerDialog);
startDatePickerInstance.init(mYearMin, mMonthMin, mDayMin, new DatePicker.OnDateChangedListener() {
    @Override
    public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
        Date maxDate = new Date(mYearMax, mMonthMax, mDayMax, 0, 0, 0);
        Date selectedDate = new Date(i, i1, i2, 0, 0, 0);
        if (selectedDate.after(maxDate)) {
            datePicker.updateDate(mYearMax, mMonthMax, mDayMax);                                        
        }
    }
}

However, the updateDate method fires onDateChanged again and the date picker is not updated.
Can anyone help to solve the problem?

like image 676
Bahri Gökcan Avatar asked May 31 '12 15:05

Bahri Gökcan


3 Answers

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...");
}
like image 166
yostyle Avatar answered Oct 29 '22 18:10

yostyle


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();
like image 33
Ramesh Bugatha Avatar answered Oct 29 '22 19:10

Ramesh Bugatha


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;
}
like image 5
Sankar V Avatar answered Oct 29 '22 17:10

Sankar V