How to show selected date in EditText using DialogFragment

I use DialogFragment to display DatePicker when user clicks EditText

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

I use this as a link.

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 click on EditText, it shows the DatePicker and the selected date in Toast . But I can not figure out how to show this date in EditText ?

+6
source share
4 answers

Based on your class structure, the best way is to create a constructor with the EditText parameter as the 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 editDate when instantiating the dialog select class.

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

Another solution is to remove DatePickerDialog.OnDateSetListener from DatePickerFragment AND implement a listener in your 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 ); } } 
+4
source

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 to display the time something like this and return the corresponding value to your fragment.

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

Hope this helps!

+1
source

Just replace this:

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

or enter this line to show the date in EditText:

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

Add the DatePickerFragment class inside the MainFragment class. Once public void onDateSet () is called by calling a method in the 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); } } } 
0
source

Source: https://habr.com/ru/post/985647/


All Articles