I have a DatePickerDialog that opens in a fragment. On first open, the dialog's default date displays today's date. The user then selects a date. When the dialog is dismissed, I set an EditText line with the user selected date.
The next time the dialog opens (because the user wants to edit their previously selected date), I want to show the user's previously selected date as the default date, not today's date. I've set up an interface from the fragment to the activity with a Bundle. Also, not sure my emptiness test on "year" in Fragment's OnCreateDialog is working properly. Please advise.
partial DatePickerFragment file:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private int currentyear;
private int currentmonth;
private int currentday;
public DatePickerFragment() {
}
public interface OnDateEnteredListener {
void OnDateEntered(int year, int month, int day);
}
OnDateEnteredListener mListener;
// Set up a Calendar object that will capture the current date for the DatePickerDialog to use.
Calendar cal = Calendar.getInstance();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle datebundle = this.getArguments();
currentyear = datebundle.get(Calendar.YEAR);
currentmonth = datebundle.get(Calendar.MONTH);
currentday = datebundle.get(Calendar.DAY_OF_MONTH);
if(currentyear != 0){
DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
dateDialog.getDatePicker().setCalendarViewShown(true);
dateDialog.getDatePicker().setSpinnersShown(false);
dateDialog.setTitle("Select a Due Date");
return dateDialog;
}
else {
// Create three variables to capture the current date.
currentyear = cal.get(Calendar.YEAR);
currentmonth = cal.get(Calendar.MONTH);
currentday = cal.get(Calendar.DAY_OF_MONTH);
// Create a DatePickerDialog which is a simple dialog containing a DatePicker. Use the current date
// as the default date in the DatePicker. The new instance of DatePickerDialog is created by passing
// 5 parameters/arguments to the constructor and returning it.
DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
dateDialog.getDatePicker().setCalendarViewShown(true);
dateDialog.getDatePicker().setSpinnersShown(false);
dateDialog.setTitle("Select a Due Date");
return dateDialog;
}
}
public void onDateSet (DatePicker view,int year, int month, int day) {
mListener.OnDateEntered(year,month,day);
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
partial Activity file:
public class Activity extends AppCompatActivity implements DatePickerFragment.OnDateEnteredListener {
int currentyear;
int currentmonth;
int currentday;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);
fListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && (fListenerEditText.getText().length() == 0)) {
DialogFragment newFragment = new DatePickerFragment();
Bundle datebundle = new Bundle();
datebundle.putInt("YEAR", currentyear);
datebundle.putInt("MONTH", currentmonth);
datebundle.putInt("DAY", currentday);
newFragment.setArguments(datebundle);
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}
});
@Override
public void onDateEntered(int year, int month, int day) {
Toast toast = Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG);
toast.show();
}
}