Randall Ma Randall Ma - 4 months ago 17
Android Question

Set Date Boundaries on Date Picker Fragment

I've got a very simple date picker fragment:

public class DatePickerFragment extends DialogFragment implements
OnDateSetListener {

public Dialog onCreateDialog(Bundle savedInstanceState) {
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);

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

public void onDateSet(DatePicker view, int year, int month, int day) {
Activity a = getActivity();

Button dateDueButton = (Button) a.findViewById(R.id.dateDue);
String sYear = Integer.toString(year).substring(2, 4);
String sMonth = Integer.toString(month + 1);
String sDay = Integer.toString(day);
dateDueButton.setText(sMonth + "/" + sDay + "/" + sYear);

I'd like to be able to set a "date boundary". For example, if the current day was Feb. 9th, I would like only dates from Feb. 10 or later to be shown on the fragment. How can I go about implementing this?


Use the minDate and maxDate parameters in the DatePicker class.

Notice that these are API level 11 classes, so you'll have to implement this functionality yourself for backwards compatibility.