Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 257
Oreo Avatar asked Apr 22 '15 05:04

Oreo


2 Answers

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 );
    }
}
like image 103
Christian Abella Avatar answered Oct 13 '22 12:10

Christian Abella


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!!

like image 41
Pranav Bhoraskar Avatar answered Oct 13 '22 12:10

Pranav Bhoraskar