brass
brass

Reputation: 612

Set android datepicker date limits

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

Upvotes: 12

Views: 17279

Answers (5)

ND1010_
ND1010_

Reputation: 3841

U can achieve this by typing a simple line

mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
        mcurrentDate.add(Calendar.MONTH,6);
        mDatePicker.getDatePicker().setMaxDate(mcurrentDate.getTimeInMillis());

Upvotes: 0

cokeby190
cokeby190

Reputation: 609

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

Upvotes: 8

Sivakumar
Sivakumar

Reputation: 633

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

Upvotes: 0

Sankar V
Sankar V

Reputation: 4863

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;
}

Upvotes: 1

MrSnowflake
MrSnowflake

Reputation: 4742

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."

Upvotes: 0

Related Questions