0

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?

SheedySheedySheedy
  • 536
  • 1
  • 6
  • 14

2 Answers2

0

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

}

Anatol
  • 941
  • 1
  • 7
  • 13
  • I am aware of that method. I have no reference to the DatePickerDialog in my activity. Only the DialogFragment - look at how the instance is created in the showDatePickerDialog method – SheedySheedySheedy Nov 20 '14 at 12:25
  • I added some code for better understanding. See updated answer. – Anatol Nov 20 '14 at 12:32
  • Thanks for your help, but that doesn't work. Cannot resolve method getDatePicker() when called on newFragment. – SheedySheedySheedy Nov 20 '14 at 12:39
  • This is because you write: `DialogFragment newFragment = new DatePickerFragment();` you should use: `DatePickerFragment newFragment = new DatePickerFragment();` I will check this tomorrow, but i think this should work. – Anatol Nov 20 '14 at 15:18
  • How can I use mDatePicker from DatePickerDialog, to modify it (ie. restrictMinDate), before showing the dialog to the user? The problem that I see is that mDatePicker will be null before calling show(). Any ideas ? – DoruAdryan Oct 25 '15 at 13:13
  • @DoruAdryan, read about passing parameters to Fragment. For example you can use this answer. http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – Anatol Nov 07 '15 at 10:00
0

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.

Community
  • 1
  • 1
SheedySheedySheedy
  • 536
  • 1
  • 6
  • 14