I\'m using the google example to insert a datepicker inside my app using a dialogfragment
http://developer.android.com/guide/topics/ui/controls/pickers.html
But
Leszek's answer works, but not if the Dialog is started from another Fragment. In that case you need to use setTargetFragment()
in the code that creates the dialog and getTargetFragment()
in the dialog code.
In the code that creates the dialog:
DialogFragment dialogFragment = new YourDialogFragment();
dialogFragment.setTargetFragment(YourParentFragment.this, 0);
dialogFragment.show(getFragmentManager(), "mydialog");
In the DialogFragment code:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
YourParentFragment parentFragment = (YourParentFragment) getTargetFragment();
// Manipulate the parent fragment
// ...
}
};
// Get the initial date
// ...
return new DatePickerDialog(getActivity(), listener, year, month, day);
}
Kotlin version.
DatePickerFragment class:
class DatePickerFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(context!!, context!! as MainActivity, year, month, day)
}
}
DatePickerDialog.OnDateSetListener is moved to MainActicity
class MainActivity : AppCompatActivity(), DatePickerDialog.OnDateSetListener {
..
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
//use date in your activity
}
}
I had a similar problem to this, but my date picker was being launched from a dialog fragment inside another fragment. This made it very difficult to play around with the callbacks, because they want to return to the Main Activity and I wanted the data to go back to the previous dialog fragment.
Initially, I passed the new date values to the Main Activity (using the OnDateSetListener) and was going to get them using the dialog that launched the date picker, but there are no lifecycle events triggered in that dialog when the datepicker closes.
The result I came to was in onDismiss in the date picker, instantiate a new dialog fragment, call a set results method on it and then launch it. Of course for this you need to make sure that the previous fragment is dismissed when it launches the date picker.
This is the dialogFragment that called the date picker
public class NewTrialDialogFragment extends DialogFragment {
public final String LOG_TAG = "NewTrialDialog Fragment";
Button startDateButton;
Integer newYear, newMonth, newDay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_new_trial, container, false);
getDialog().setTitle("Dialog New Trial");
startDateButton = (Button) rootView.findViewById(R.id.button_start_date);
startDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(v);
}
});
//If you exit the datepicker without choosing anything, it returns zeros
if (newYear != null && newMonth != null && newDay != null && newYear != 0) {
startDateButton.setText(newYear + " " + newMonth + " " + newDay);
}else{
startDateButton.setText("Select Start Date");
}
return rootView;
}
public void showDatePickerDialog(View v) {
TrialDatePickerDialog newDialog = new TrialDatePickerDialog();
newDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
this.dismiss();
}
public void setDates(int year, int month, int day){
newYear = year;
newMonth = month;
newDay = day;
}}
And the date picker
public class TrialDatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private final String LOG_TAG = "TrialDatePickerDialog";
int newYear, newMonth, newDay;
@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 TrialDatePickerDialog and return it
return new DatePickerDialog(getActivity(), this , year, month, day);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
FragmentManager fm = getActivity().getSupportFragmentManager();
NewTrialDialogFragment newTrialDialogFragment = new NewTrialDialogFragment();
newTrialDialogFragment.setDates(newYear, newMonth, newDay);
newTrialDialogFragment.show(fm, "new_trial_dialog");
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
newYear = year;
newMonth = monthOfYear;
newDay = dayOfMonth;
}
}