12

I am using datePicker in android to display images based on user selected dates. I need to limit said dates to certain days for instance Jan 1st 2010 to Dec 31st 2010. Simple as that i thought but no where can i find the answer on how to limit these dates.

Does anyone know how to limit the dates for Android DatePicker

brass
  • 612
  • 2
  • 5
  • 15
  • 1
    I managed to programatically set the min, max selectable dates, & also set the current ( default ) date for the DatePicker View : http://stackoverflow.com/questions/6421874/how-to-get-the-date-set-in-the-datepicker-widget-in-android/7961268#7961268 –  Nov 02 '11 at 14:39

5 Answers5

8

You can just set :

dateDialog.getDatePicker().setMaxDate(new Date().getTime());

and

dateDialog.getDatePicker().setMinDate(new Date().getTime());

where dateDialog is a

new DatePickerDialog()

and the param type to set for MaxDate and MinDate is a long

cokeby190
  • 609
  • 7
  • 14
1

It might be too late to answer, but the it is easy 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;
}
Sankar V
  • 4,794
  • 3
  • 38
  • 56
0

Why you should not design new layout like DatePicker with customoption. You can do with Spinners and Forloops.

Sivakumar
  • 633
  • 4
  • 9
0

I guess you have to implement your own DatePicker. The logic behind this would be: "Only a few developers would ever limit the DatePicker so they have to implement the functionality themselfs."

MrSnowflake
  • 4,724
  • 3
  • 29
  • 32
  • 1
    Also for XML there are some additional properties available like endYear beginYear, you can use that for bounds. – Pentium10 Mar 26 '10 at 20:12
  • where would i find the information on the endYear beginYear, sounds like that would work pretty nicely. I was afraid of the answer implement my own, hopefully this xml properties will pan out. – brass Mar 26 '10 at 20:50
  • the endYear and beginYear bounds only work with widget not the actual datePicker dialog i am using. I will look to see how to implement the widget instead, unless anyone can offer info on how to apply bounds to the dialog – brass Mar 30 '10 at 20:02
0

U can achieve this by typing a simple line

mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
        mcurrentDate.add(Calendar.MONTH,6);
        mDatePicker.getDatePicker().setMaxDate(mcurrentDate.getTimeInMillis());
ND1010_
  • 3,743
  • 24
  • 41