Search code examples
androidlistenerandroid-datepicker

DatePickerDialog listener


I am making an Android app where I use a DatePickerDialog. The intended function is for the user to select a date in the dialog, and then for a TextView to reflect that chosen date. However, I can't find any sort of a click listener to notify my Activity when the user has selected a date. I am looking for a way to detect when the user has selected a date, but from my main Activity. This is my DatePickerDialog class:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private GregorianCalendar date;

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

@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    date = new GregorianCalendar(year, month, day);
}

public GregorianCalendar getDate() {
    return date;
}
}    

And the code where I launch the dialog:

DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getSupportFragmentManager(), "datePicker");

I currently launch the dialog from a class that extends Activity, and I am looking for a way to detect from within that class if a user has selected a date from the dialog. Any suggestions?


Solution

  • I recommend reading: Communicating with Other Fragments.

    Activity has to implement DatePickerDialog.OnDateSetListener interface:

    public class MyActivity extends ActionBarActivity implements DatePickerDialog.OnDateSetListener {
        ...
    
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // the user selected, now you can do whatever you want with it
        }
    }
    

    Activity will be notified when user selected date if you add following code to DialogPickerFragment class:

    public class DatePickerFragment extends DialogFragment {
        private DatePickerDialog.OnDateSetListener mListener;
    
        @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(), mListener, year, month, day);
        }
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
    
            if (!(context instanceof DatePickerDialog.OnDateSetListener)) {
                throw new IllegalStateException("Activity must implement fragment's callbacks.");
            }
    
            mListener = (DatePickerDialog.OnDateSetListener) activity;
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
    
            mListener = null;
        }
    }