Search code examples
androidandroid-datepicker

How to set the limit on date in Date picker dialog


I want to put the limit on date so that user can not pick date more then that, for example if today is 1 January then User should not be able to select more then 7 dates , I mean he can not select 9 January. I also want him not to select the month and year. So I am putting a limit to set his task in one week.

what I have done so far is showing the date picker fragment and setting current date in it. the code in my main activity goes like this:

etSelectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment datePickerFragment = new DatePickerFragment() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Log.d("Date Change", "onDateSet");
                        Calendar c = Calendar.getInstance();
                        c.set(year, month, day);
                        DateFormat df = DateFormat.getDateInstance();
                        etSelectDate.setText(df.format(c.getTime()));
                        //nextField.requestFocus(); //moves the focus to something else after dialog is closed

                    }
                };
                datePickerFragment.show(MainActivity.this.getSupportFragmentManager(), "datePicker");

            }
        });

and date picker fragment class goes like this :

 public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            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);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            //blah
        }
    }

till then its is working fine , but I do not know how to put the limit on date and rest of the months and year should be non Select able . I have seen many link such as like this , but I do not understand How can I do that and also there is nothing helpful on android site.

So please help me , How can I put limit of seven days only

Update

Through your replies I know how to set the max date in calender , so As I want to set the max date 7 days ahead of current date , I am still not getting it. the method I read so far is :

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

It is setting the current date as maximum, but How can I add 7 days ahead in it since it is Date object ? please help


Solution

  • You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.

    dpdialog.getDatePicker().setMinDate(minDate);  
    dpdialog.getDatePicker().setmaxDate(maxDate);  
    

    Source :Set Limit on the DatePickerDialog in Android?

    You can calculate the minDate by using,

    Date today = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(today);
    c.add( Calendar.MONTH, -6 ) // Subtract 6 months
    long minDate = c.getTime().getTime() // Twice!  
    

    Updated :

    Replace the below line

    return new DatePickerDialog(getActivity(), this, year, month, day);
    

    with

     // Create a new instance of DatePickerDialog and return it
        DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
        pickerDialog.getDatePicker().setMaxDate(maxDate);
        pickerDialog.getDatePicker().setMinDate(minDate);
        return pickerDialog;