Oreo
Oreo

Reputation: 2594

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 ?

Upvotes: 6

Views: 2794

Answers (4)

Christian Abella
Christian Abella

Reputation: 5797

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

Upvotes: 4

Rakhi
Rakhi

Reputation: 81

Add the DatePickerFragment class inside the MainFragment class. Once the public void onDateSet()is called invoke a method in same class to update the editText field.

public MainFragment extents Fragment{

        .....
         updateDateField(String formattedDate){
              editDate.setText(formattedDate);
         }

        public class DatePickerFragment extends DialogFragment
                   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());
                                updateDateField(formattedDate);
            }
        }
    }

Upvotes: 0

Pranav Bhoraskar
Pranav Bhoraskar

Reputation: 135

You can do something like this -

EditText edttxt_date = (EditText) findViewById(R.id.date);
edttxt_date.setText(formattedDate);
Toast.makeText(getBaseContext(), formattedDate, Toast.LENGTH_SHORT).show();

or you can use Calender and DatePickerDialogFragment too to display time something like this and return the corresponding value to your fragment.

DateFormat dateFormat = DateFormat.getDateInstance();
edttxt_date.setText(dateFormat.format(cal.getTime()));

Hope it helps!!

Upvotes: 1

S Sathiya
S Sathiya

Reputation: 47

Just replace this :

Toast.makeText(getActivity(), formattedDate, Toast.LENGTH_LONG).show(); 

line or enter this line to show the date in your EditText :

EditText date=(EditText)find(...);
date.setText(formattedDate);

Upvotes: 0

Related Questions