1

This question describes how to do it using a DatePicker widget, but I haven't got anything like that.

I have these:

@Override
protected Dialog onCreateDialog(int id)
{

    switch (id)
    {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear1, mMonth1, mDay1);
    case DATE_DIALOG_ID_2:
        return new DatePickerDialog(this, mDateSetListener2, mYear2, mMonth2, mDay2);
    case DATE_DIALOG_ID_3:
        return new DatePickerDialog(this, mDateSetListener3, mYear3, mMonth3, mDay3);
    }
    return null;
}


protected void onPrepareDialog(int id, Dialog dialog)
{

    switch (id)
    {
    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(mYear1, mMonth1, mDay1);
        break;
    case DATE_DIALOG_ID_2:
        ((DatePickerDialog) dialog).updateDate(mYear2, mMonth2, mDay2);
        break;
    case DATE_DIALOG_ID_3:
        ((DatePickerDialog) dialog).updateDate(mYear3, mMonth3, mDay3);
        break;
    }
}

and then three DatePickerDialog.OnDateSetListener() s

There is no setMax() or setMin() on these DatePickerDialogs.

Thanks in advance.

Community
  • 1
  • 1
MSpeed
  • 8,153
  • 7
  • 49
  • 61

2 Answers2

9

Just implement your own DatePickerDialog, that changes the date back to a allowed date if the user tries to change it below the minimum date.

A stripped version of the one I am using is:

public class MyDatePickerDialog extends DatePickerDialog{

private Date maxDate;
private Date minDate;

public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
    super(context, callBack, year, monthOfYear, dayOfMonth);        
    init(year, monthOfYear, dayOfMonth);
}

public MyDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear,    int dayOfMonth) {
    super(context, theme, callBack, year, monthOfYear, dayOfMonth);
    init(year, monthOfYear, dayOfMonth);
}

private void init(int year, int monthOfYear, int dayOfMonth){
    Calendar cal = Calendar.getInstance();

    cal.set(1970, Calendar.JANUARY, 1);
    minDate = cal.getTime();

    cal.set(3000, Calendar.JANUARY, 1);
    maxDate = cal.getTime();

    cal.set(year, monthOfYear, dayOfMonth);
}

public void onDateChanged (final DatePicker view, int year, int month, int day){
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, day);
    Date currentDate = cal.getTime();

    final Calendar resetCal = cal; 
    if(!minDate.before(currentDate) ){
        cal.setTime(minDate);
        view.updateDate(resetCal.get(Calendar.YEAR), resetCal.get(Calendar.MONTH), resetCal.get(Calendar.DAY_OF_MONTH));

    }else if(maxDate.before(currentDate)){
        cal.setTime(maxDate);
        view.updateDate(resetCal.get(Calendar.YEAR), resetCal.get(Calendar.MONTH), resetCal.get(Calendar.DAY_OF_MONTH));
    }       
}

public void setMaxDate(Date date){
    this.maxDate = date;
}

public void setMinDate(Date date){
    this.minDate = date;
}   

}

thaussma
  • 9,756
  • 5
  • 44
  • 46
  • Nice. Any special reason to use `resetCal`? (And for future readers: calling `view.updateDate` will also invoke the `onDateChanged` another time. One might also need to care of time components, like `Calendar.getInstance()` includes the current hour, minutes and seconds, while `Calendar#set(year, month, day)` [does not change those](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set(int,%20int,%20int)). – Arjan Jun 03 '12 at 14:22
  • Cool, thanks! For anyone else reading: The only thing I had to change here was set the `minDate` to the current date in the `init()` method. To do this I just deleted the line `cal.set(1970, Calendar.JANUARY, 1);` Thanks again! – MSpeed Jun 11 '12 at 12:17
  • I think that just setting the min and max dates should be okay. – lemon Dec 09 '12 at 05:04
  • Thanks a lot. It helps lot in cases of API lower than 11. – Chintan Raghwani Jan 03 '13 at 11:08
  • Thanks you very much for you code,but I get Stack-overflow exception when i took maximum year value 2100. – void Sep 02 '13 at 11:34
  • Thank you so much, this helped me with my problem :-) – Ariel Magbanua Oct 23 '13 at 08:36
0

my first answer. I hope everything works. I have the same problem with the datepicker embedded in an acitivity. Based on this answer I got this solution but only for Maximum Date.

Works fine in the emulator also for ICS.

// Get the Date for INIT
Calendar c = Calendar.getInstance();
int INIT_JAHR = c.get(Calendar.YEAR);
int INIT_MONAT = c.get(Calendar.MONTH);
int INIT_TAG = c.get(Calendar.DAY_OF_MONTH);
this.dp_quellen_datum.init(INIT_JAHR, INIT_MONAT, INIT_TAG,
    new DatePicker.OnDateChangedListener() {

        public void onDateChanged(DatePicker datePicker,
                int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method sub
            Calendar cal = Calendar.getInstance();
            Calendar maxCal = Calendar.getInstance();
            // Actual date from datepicker
            cal.set(year, monthOfYear, dayOfMonth);
            // maximum Date in my Case today
            maxCal.set(maxCal.get(Calendar.YEAR),
                    maxCal.get(Calendar.MONTH),
                    maxCal.get(Calendar.DAY_OF_MONTH));
            Log.d("MAX -> Aktuell", maxCal.getTime()
                    + "-->"
                    + cal.getTime()
                    + "===>"
                    + maxCal.getTime()
                            .before(cal.getTime()));
            // Set datepicker to today if selected date in the feature
            final Calendar resetCal = cal; // <- Don't tested if this line is needed
            if (maxCal.getTime().before(cal.getTime()) == true) {
                cal.setTime(maxCal.getTime());
                Log.d("Anpassen", "AKTIV");
                datePicker.updateDate(
                        resetCal.get(Calendar.YEAR),
                        resetCal.get(Calendar.MONTH),
                        resetCal.get(Calendar.DAY_OF_MONTH));
            }

        }
    });
JackTools.Net
  • 734
  • 6
  • 13