Android Dialogfragment - Get Reference To Date Picker
The Android docs tell us to implement date pickers like so, extending DialogFragment: public static class DatePickerFragment extends DialogFragment implemen
Solution 1:
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
publicstaticclassDatePickerFragmentextendsDialogFragmentimplementsDatePickerDialog.OnDateSetListener {
private DatePicker mDatePicker;
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the pickerfinalCalendarc= Calendar.getInstance();
intyear= c.get(Calendar.YEAR);
intmonth= c.get(Calendar.MONTH);
intday= c.get(Calendar.DAY_OF_MONTH);
DatePickerDialogdialog=newDatePickerDialog(getActivity(), this, year, month, day);
mDatePicker = dialog.getDatePicker();
return dialog;
}
publicvoidonDateSet(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.
publicvoidshowDatePickerDialog(View v) {
DatePickerFragmentnewFragment=newDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
DatePickerpicker= newFragment.getDatePicker();
}
Solution 2:
I sorted this out with the help of this StackOverflow answer:
publicvoidshowDatePickerDialog() {
DialogFragmentnewFragment=newDatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
getFragmentManager().executePendingTransactions(); // commits the show method from aboveDatePickerDialogdialog= (DatePickerDialog) newFragment.getDialog();
DatePickerdatePicker= (DatePicker) dialog.getDatePicker();
Datenow=newDate();
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.
Post a Comment for "Android Dialogfragment - Get Reference To Date Picker"