16

How to Hide past dates on DatePicker dialog ? I don't wanna allow user to select past dates

What would be the best way ? if i want to restrict user to select future dates only !

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        .................................................
        editDate.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DIALOG_DATE);
            }
        });   

    }

     @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_DATE:
                return new DatePickerDialog(this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        dateTime.set(year, monthOfYear, dayOfMonth);
                        editDate.setText(dateFormatter
                                .format(dateTime.getTime()));
                    }
                }, dateTime.get(Calendar.YEAR),
                   dateTime.get(Calendar.MONTH),
                   dateTime.get(Calendar.DAY_OF_MONTH));
            }
            return null;
     }

}
Sun
  • 6,768
  • 25
  • 76
  • 131

5 Answers5

10

You have to use setMinDate for minimum date and you can also use setMaxDate if you want to set maximum date.

Here is sample code for SetMinDate for DatePickerDialog.

@Override
protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DATE:

            DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    dateTime.set(year, monthOfYear, dayOfMonth);
                    editDate.setText(dateFormatter.format(dateTime.getTime()));
                }
            }, dateTime.get(Calendar.YEAR), dateTime.get(Calendar.MONTH), dateTime.get(Calendar.DAY_OF_MONTH));
            Calendar mCalendarMinimum = Calendar.getInstance();
            mCalendarMinimum.add(Calendar.HOUR, -1);
            mDatePickerDialog.getDatePicker().setMinDate(mCalendarMinimum.getTimeInMillis());
            return mDatePickerDialog;
        }
        return null;
    }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
9

You can use setMinDate (long minDate) to achieve your goal. Doc says:

Sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
2

U can try this code in your switch case

DatePickerDialog dpd = new DatePickerDialog(AddTaskActivity.this,
                    myDateListener, year, month, day);
            dpd.getDatePicker().setCalendarViewShown(false);
            dpd.show();

Where myDateListener is following code

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {

        showDate(arg1, arg2 + 1, arg3);
    }
};

and showDate() is the method to display the date in textView Here is the code for showDate()

 public void showDate(int year, int month, int day) {

    String day1 = String.valueOf(day);
    String month1 = String.valueOf(month);



   String  s2 = day1 + "/" + month1 + "/" + year;

    Log.e("date", s2);
    Cal.setText(s2);
}

Hope it will help you :)

1

You can solve it with one line of code:

datePicker.setMinDate(Calendar.getInstance().getTimeInMillis());

This way, the Date Picker will show only dates from the current time and forward.

Reference.

George
  • 6,886
  • 3
  • 44
  • 56
0
this.calendar = new GregorianCalendar();
this.datePicker = (DatePicker) findViewById(R.id.xxxxx);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // (picker is a DatePicker)
    this.datePicker.setMinDate(this.calendar.getTimeInMillis());
} else {
    final int minYear = this.calendar.get(Calendar.YEAR);
    final int minMonth = this.calendar.get(Calendar.MONTH);
    final int minDay = this.calendar.get(Calendar.DAY_OF_MONTH);

    this.datePicker.init(minYear, minMonth, minDay,
            new OnDateChangedListener() {

                public void onDateChanged(DatePicker view, int year,
                        int month, int day) {
                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, month, day);

                    if (calendar.after(newDate)) {
                        view.init(minYear, minMonth, minDay, this);
                    }
                }
            });
    Log.w(TAG, "API Level < 11 so not restricting date range...");
}

Hope it will help you :)

Joy Helina
  • 378
  • 1
  • 5
  • 17