6

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 ?

Oreo
  • 2,586
  • 8
  • 38
  • 63

4 Answers4

4

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 );
    }
}
Christian Abella
  • 5,747
  • 2
  • 30
  • 42
  • The newer version of Android SDK forces you to get a empty, no-args constructor. Check this issue: http://stackoverflow.com/questions/29762949/error-this-fragment-should-provide-a-default-constructor-a-public-constructor – Oreo Apr 22 '15 at 06:16
  • I have run that issue before. It is really just a warning. If you want to remove that then just add another constructor without an argument and call super(); inside the constructor. – Christian Abella Apr 22 '15 at 06:18
  • It is just a protection just in case you will use the class in an XML layout where they will use the default constructor to create an instance of your object when the XML is inflated. We are allowed to create different constructors for our classes as long as we know what we are doing. – Christian Abella Apr 22 '15 at 06:22
  • but whenever i am generating apk in Android Studio getting this error – Oreo Apr 22 '15 at 06:26
  • i have posted XML, check now – Oreo Apr 22 '15 at 06:35
  • do you have an XML for your DatePickerFragment? – Christian Abella Apr 22 '15 at 06:40
  • i am not using any xml for DatePickerFragment, so you saying its only warning, so may i know How can i disable this kind of warnings in Android Studio ? – Oreo Apr 22 '15 at 06:48
  • http://stackoverflow.com/questions/17603677/supported-suppresswarnings-values-in-android-studio – Christian Abella Apr 22 '15 at 06:51
  • i have used your old way and in build.gradle : lintOptions { abortOnError false checkReleaseBuilds false } – Oreo Apr 22 '15 at 08:52
1

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

Pranav Bhoraskar
  • 115
  • 1
  • 13
0

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);
Abhishek Ghosh
  • 2,593
  • 3
  • 27
  • 60
S Sathiya
  • 47
  • 7
  • but i am using EditText in Fragment, and formattedDate is in DatePickerFragment.java – Oreo Apr 22 '15 at 05:39
0

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);
            }
        }
    }
Rakhi
  • 81
  • 12
  • I have tried your way, but getting: Error:(121) Error: This fragment inner class should be static (MainFragment.DatePickerFragment) [ValidFragment] – Oreo Apr 22 '15 at 06:47
  • Try to make the DatePickerFragment static – Rakhi Apr 22 '15 at 06:48
  • Else try to use callback Listener . Write DatePickerFragment as new class. – Rakhi Apr 22 '15 at 06:49