How to create datePicker and timePicker dialogs in fragment class?
android
Solution
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(" "));
}
Problem
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?