9

I am using Android's default date picker and my min supported SDK is 10 and I want to set the min and the max dates of the date picker.

Here is what I have in my MainActivity class after the onCreate method:

private void registerButtonListeners() {
    Calendar.setOnClickListener(new View.OnClickListener() 
    {   
        @Override
        public void onClick(View v) {
            showDialog(DATE_PICKER_DIALOG);
        }
    });
}

@Override
protected Dialog onCreateDialog(int id)
{
    switch(id)
    {
    case DATE_PICKER_DIALOG: return showDatePicker();

    }
    return super.onCreateDialog(id);
}
private DatePickerDialog showDatePicker()
{
        DatePickerDialog datePicker= new DatePickerDialog(MainActivity.this,android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth,new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                mCalendar.set(Calendar.YEAR,year);
                mCalendar.set(Calendar.MONTH,monthOfYear);
                mCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                updateDateText();

            }
        },mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        {
            long d=1356994800000L;
            //datePicker.getDatePicker().setMinDate(d);
            //datePicker.getDatePicker().setMaxDate(1546210800000L);

        }
        return datePicker;
}
3

3 Answers 3

12

You can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using:

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 pass current date and add or substract x years from that..

1
  • 5
    but I belive this is supported api 11 and up....that's why i mentioned that i use min sdk 10 and also at the end of my code you can see what i've commented //datePicker.getDatePicker().setMinDate(d); //datePicker.getDatePicker().setMaxDate(1546210800000L);
    – denza
    Dec 28, 2013 at 10:37
1

In the default DatePicker of iOS it provides the feature of setting min. and max. date for the Date picker. The same feature is also available in Android application development but it is available for Android API 11 and above. It means that the feature of min and max date range won’t work in devices below Android API 11.

To overcome this kind of issue and to support this feature in all the Android devices including devices below Android API 11, we had to extend the “DatePickerDialog” class for providing the facility of setting the min and max date range that can be selected. Check the link given below-

How to make a Custom DatePicker control with maximum and minimum date range in Android application

For explanation of the implementation please check below link-

Explain the implementation of Custom DatePicker in our application

1

i solved this by following

DatePickerDialog datePickerDialog;
final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
    datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());    

hope it will help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.