0
votes

The Android docs tell us to implement date pickers like so, extending DialogFragment:

public static class DatePickerFragment extends DialogFragment
                        implements DatePickerDialog.OnDateSetListener {

    @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);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}

And then to display the date picker dialog in our Activity, we call this method:

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

What I need to know is, how do I get a reference to the actual date picker used in the dialog so that I may use methods such as .setMinDate() and .setMaxDate()? Any suggestions?

2

2 Answers

0
votes

http://developer.android.com/reference/android/app/DatePickerDialog.html

In onCreateDialog(Bundle savedInstanceState) you returning reference on DatePickerDialog. return new DatePickerDialog(getActivity(), this, year, month, day);

DatePickerDialog has method getDatePicker () you can use this to get DatePicker.

UPDATE

public static class DatePickerFragment extends DialogFragment
                    implements DatePickerDialog.OnDateSetListener {

private DatePicker mDatePicker;

@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);

    DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
    mDatePicker = dialog.getDatePicker();

    return dialog;
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
}

public DatePicker getDatePicker() {
    return mDatePicker
}

}

And now you can access this datePicker.

public void showDatePickerDialog(View v) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
DatePicker picker = newFragment.getDatePicker();

}

0
votes

I sorted this out with the help of this StackOverflow answer:

public void showDatePickerDialog() {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
    getFragmentManager().executePendingTransactions();     // commits the show method from above
    DatePickerDialog dialog = (DatePickerDialog) newFragment.getDialog();
    DatePicker datePicker = (DatePicker) dialog.getDatePicker();
    Date now = new Date();
    datePicker.setMaxDate(now.getTime());
}

You need to call .executePendingTransactions()on the fragment manager to stop the dialog fragment being added asynchronously, otherwise the getDialog() method gives a null pointer exception.

I am putting it in here for anyone in the future who may have this problem.