1

I don't want to accept past dates in DatePicker. Here is my code, which I am using to show datepicker and to set the date in EditText.

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        public EditText editDate;
        private Calendar dateTime = Calendar.getInstance();
        private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMMM yyyy");

    public DatePickerFragment(EditText editText) {
        editDate = editText;
    }

    @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 monthOfYear, int dayOfMonth) {
            dateTime.set(year, monthOfYear, dayOfMonth);
            editDate.setText(dateFormatter
                    .format(dateTime.getTime()));
        }

}
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
Oreo
  • 2,586
  • 8
  • 38
  • 63

1 Answers1

2

Set Min Date For DatePicker

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the today 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
        DatePickerDialog dp = new DatePickerDialog(getActivity(), this, year, month, day);
        // Use the today date as the Min date in the picker 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            dp.getDatePicker().setMinDate(c.getTimeInMillis());
        } else {
            Log.w(TAG, "API Level < 11 so not restricting date range...");
        }            
        return dp;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
  • 1
    getting Call requires minimum API level 11 current is 10 – Oreo May 01 '15 at 10:37
  • 1
    If your app supports versions of Android lower than 3.0, be sure that you call getSupportFragmentManager() to acquire an instance of FragmentManager. Also make sure that your activity that displays the time picker extends FragmentActivity instead of the standard Activity class. Statement from http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker link. – Dhaval Patel May 01 '15 at 10:42
  • @Oreo change manifest to this – Maveňツ May 01 '15 at 10:43
  • @Oreo Take a look on http://stackoverflow.com/questions/10836679/android-datepicker-min-max-date-before-api-level-11. – Dhaval Patel May 01 '15 at 10:47
  • yeah looked not useful, is there any other solution ?? – Oreo May 01 '15 at 10:54