Search code examples
androidandroid-fragmentsandroid-datepicker

How do I change a DatePickerDialog's default date from today's date to a user selected date?


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, I want to show the user's previously selected date, not today's date. Should I create a Bundle in onDateSet function to save the selected date? If so, how do I use the Bundle to populate the dialog the next time it is opened? Would an emptiness test on the EditText line be useful so that the dialog does not populate with today's date if the line has a date already set?

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

Solution

  • When the user picks the date, use the following to communicate the data back to your Activity once the dialog closes (communicating data from a fragment back to the starting activity): http://developer.android.com/training/basics/fragments/communicating.html

    Once you receive the values back in your activity, use the following when starting your DatePickerFragment:

    DatePickerFragment datepickerfragment = new DatePickFragment();
    
    Bundle bundle = new Bundle();
    bundle.putInt("YEAR", currentyear);
    bundle.putInt("MONTH", currentmonth);
    bundle.putInt("DAY", currentday);
    datepickerfragment.Arguments = bundle;
    
    fragmentmanager.BeginTransaction().Add(R.layout.container, datepickerfragment).Commit();
    

    Where currentyear, currentmonth, and currentday are the calendar values the user selected. Retrieve the values in your dialog fragment next time it opens as follow:

    int year = this.Arguments.getInt("YEAR");
    int month = this.Arguments.getInt("MONTH");
    int day = this.Arguments.getInt("DAY");
    

    Now you can set the dialog's date to year, month, day obtained from the previous DatePickerFragment's values.

    EDIT: To set the values, in onCreateDialog(Bundle bundle) do as follows:

    ....
    int year = this.Arguments.getInt("YEAR");
    int month = this.Arguments.getInt("MONTH");
    int day = this.Arguments.getInt("DAY");
    
    if(year != null){
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }else{
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }