Search code examples
androiddatepickerandroid-edittextandroid-dialogfragment

How to show Selected Date in EditText using DialogFragment


I am using DialogFragment to show DatePicker when user taps on EditText

How can i show the selected date in the same EditText.

I am using this as a reference.

DatePickerFragment.java:

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @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 monthOfYear, int dayOfMonth) {
        Calendar c = Calendar.getInstance();
        c.set(year, monthOfYear, dayOfMonth);

        SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
        String formattedDate = sdf.format(c.getTime());
        Toast.makeText(getActivity(), formattedDate, Toast.LENGTH_LONG).show();
    }

}

Fragment:

editDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                android.support.v4.app.DialogFragment dialogFragment = new DatePickerFragment();
                dialogFragment.show(getFragmentManager(), "datePicker");
            }
        });

When I tap on EditText, it shows DatePicker and selected date in a Toast. But I can't figure out how to show that date in the EditText ?


Solution

  • Based on your class structure, the best way to go is to create a constructor with EditText as parameter.

    private EditText mEditText;
    ...
    public DatePickerFragment(EditText editText)
    {
      mEditText = editText;
    }
    
     @Override
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
    {
      Calendar c = Calendar.getInstance();
      c.set(year, monthOfYear, dayOfMonth);
    
      SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
      String formattedDate = sdf.format(c.getTime());
      Toast.makeText(getActivity(), formattedDate, Toast.LENGTH_LONG).show();
    
      mEditText.setText( formattedDate );
    }
    

    Then pass the editDate when you create an instance of the dialog picker class.

    public void onClick(View v) 
    {
      android.support.v4.app.DialogFragment dialogFragment = new DatePickerFragment(editDate);
    dialogFragment.show(getFragmentManager(), "datePicker");
    }
    

    Another solution is to remove the DatePickerDialog.OnDateSetListener from DatePickerFragment And implement the listener in you fragment.

    public class OtherFragment extends Fragment
            implements DatePickerDialog.OnDateSetListener 
    {
       @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar c = Calendar.getInstance();
            c.set(year, monthOfYear, dayOfMonth);
    
            SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
            String formattedDate = sdf.format(c.getTime());
            Toast.makeText(getActivity(), formattedDate, Toast.LENGTH_LONG).show();
            editDate.setText( formattedDate );
        }
    }