How to create datePicker and timePicker dialogs in fragment class?

I want to know is there a way to create a datePicker in a fragment? I am creating one the regular activity may and it gives me syntax error. What is the correct way to do this?


Solution 1:

Expanding on Codejoy's answer:

Here is my DatePickerDialogFragment class:

public class DatePickerDialogFragment extends DialogFragment {
    private Fragment mFragment;

    public DatePickerDialogFragment(Fragment callback) {
        mFragment = callback;
    }

     public Dialog onCreateDialog(Bundle savedInstanceState) {
         return new DatePickerDialog(getActivity(), (OnDateSetListener) mFragment, 1980, 7, 16);
     }
}

(Notice that the constructor accepts the fragment that is using this dialog - and that we use this reference for the callback listener field for DatePickerDialog)

My fragment then just implements onDateSetListener:

public class SignupFragment extends Fragment implements OnDateSetListener {
...
public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
    // do stuff with the date the user selected
}
}

... and then I show the dialog from my Fragment like so:

FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = new DatePickerDialogFragment(this);
newFragment.show(ft, "dialog");

Not sure if this is the best way to do it, but seems to work fine.

Solution 2:

Though above answers might work, I believe I have a much easier solution:

DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 
                             2000, 1,1);
dialog.show();

Where datePickerListerener is something like this:

private DatePickerDialog.OnDateSetListener datePickerListener 
            = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
            //Do whatever you want
        }
};

Solution 3:

Previous anwsers are explaining good enough but here is how I changed the view onDateSet method of DialogFragment.

You can pass the view you want to change to the constructer of class extending DialogFragment. Then call setText method onDateSet method of the class. Here is the code:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

public EditText activity_edittext;

public DatePickerFragment(EditText edit_text) {
    activity_edittext = edit_text;
}

@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 view, int year, int month, int day) {
        activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(year));
    }
}

Then you can show your fragment like this:

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

Set this method onClick method of EditText you want to change. Like this:

    <EditText
            android:id="@+id/editTextToShowDate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:focusable="false"
            android:inputType="date"
            android:onClick="showDatePickerDialog" >
   </EditText>

Solution 4:

You will most likely need to use a DialogFragment. I found some information here:

Show dialog from fragment?

and also a big help here:

https://github.com/commonsguy/cw-advandroid/blob/master/Honeycomb/FeedFragments/src/com/commonsware/android/feedfrags/AddFeedDialogFragment.java

This should help you get on your way, I am doing this very thing now. Though inside the example code I don't use a builder and instead just return:

return new DatePickerDialog(getActivity(), mDateSetListener, mYear, mMonth, mDay);

This seems to work... though I cannot figure out yet how to update the text on the fragment that calls this DialogFragment. I thought this would work and it doesn't:

 public void updateDisplay()
 {
     //update our button text from the calling fragment, this isn't quite working
     //doesn't crash just doesn't update...must be missing something.
     View v=getActivity()
        .getLayoutInflater()
        .inflate(R.layout.session_edits, null);
     Button sessionDate = (Button)v.findViewById(R.id.sessionPickDate);
            sessionDate.setText(new StringBuilder()
                            .append(mMonth+1).append("-").append(mDay).append("-").append(mYear).append(" "));

 }