0

I want to disable my past dates in DatePicker and only show current and future dates. I am using the code below but this will display past dates also, please suggest how do i changes the code.

public void SelectDate(View view) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "DatePicker");
}

public void populateSetDate(int year, int month, int day) {
    EditDate = (EditText) findViewById(R.id.editDate);
    EditDate.setText(day + "/" + month + "/" + year);
}

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm + 1, dd);
    }
}
Tan Zc
  • 11
  • 3
  • 3
    Duplicate:- http://stackoverflow.com/questions/23762231/how-to-disable-past-dates-in-android-date-picker – Joy Oct 20 '16 at 14:46

2 Answers2

0

Use setMinDate() setMaxDate() methods of DatePicker. Use current date as parameter for setMinDate() and current date + 10 years for setMaxDate().

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
0

You can set a min date on the DatePicker using setMinDate()

for Example:

datePicker.setMinDate(System.currentTimeMillis() - 1000);

I removed one second because setting min date to now might produce error since it has to be in the past and not the present

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66