Archie.bpgc
Archie.bpgc

Reputation: 24012

Android: get DatePickerDialog in a fragment

Previously i was using showDialog() which now is deprecated.

I am already in a Fragment which contains an EditText. How can i call this DatePickerDialog and set the EditText's Text to the selected date?

Using DialogFragment seems a bit complicated.

Thank You

Upvotes: 3

Views: 6709

Answers (2)

Armin
Armin

Reputation: 619

Please take a look at android API guides : http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

Upvotes: 0

moskis
moskis

Reputation: 1156

Its easy:

DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year,
            int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

DatePickerDialog d = new DatePickerDialog(getActivity(),
        R.style.MyThemee, mDateSetListener, mYear, mMonth, mDay);
d.show();

UpdateDisplay method:

private void updateDisplay() {

    GregorianCalendar c = new GregorianCalendar(mYear, mMonth, mDay);
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");

    editDate.setText(sdf.format(c.getTime()));

    sdf = new SimpleDateFormat("yyyy-MM-dd");

    transDateString=sdf.format(c.getTime());
}// updateDisplay

Upvotes: 14

Related Questions