0

I have a datepicker dialogon my android app but I want the user to not be able to pick a previous date. How do I achieve this?

My code

inputStart.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                int mYear, mMonth, mDay;

                final Calendar c = Calendar.getInstance();
                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dpd = new DatePickerDialog(Leave.this,
                    new DatePickerDialog.OnDateSetListener() {
                        public void onDateSet(DatePicker view, int year, int month, int day) {

                            c.set(year, month, day);
                            String date = new SimpleDateFormat("yyyy/MM/dd").format(c.getTime());
                            inputStart.setText(date);

                            int mYear = c.get(Calendar.YEAR);
                            int mMonth = c.get(Calendar.MONTH);
                            int mDay = c.get(Calendar.DAY_OF_MONTH);
                        }
                    }, mYear, mMonth, mDay);
                dpd.show();
            }
        });

1 Answers1

0

There is a method called setMinDate (since API level 11 i think) that sets the minimal date supported by the NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

DatePicker.setMinDate(new Date().getTime())
Manos
  • 1,471
  • 1
  • 28
  • 45