0

How Can I open Date Picker Dialog?

I tried using this code, but this doesn't do anything. I believe it has been deprecated, but I haven't found the solution yet.

mDOB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);

        }
    });
stanley santoso
  • 343
  • 1
  • 6
  • 19

1 Answers1

1
class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            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);

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // get month, year and day
        }
    }

and call this dialog by using this...

new DatePickerFragment().show(getFragmentManager(), "datepicker");
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • your code works. Thank you. the documentation says I need to use getSupportFragmentManager. That doesn't work for me but getFragmentManager works. do you know why? – stanley santoso Aug 25 '15 at 11:19
  • see this thread http://stackoverflow.com/questions/25306197/using-getfragmentmanager-vs-getsupportfragmentmanager – Deepak Goyal Aug 25 '15 at 14:52