Quantcast
Viewing latest article 8
Browse Latest Browse All 10

Android Date Picker DialogFragment

In this post we will discuss about creating datepicker using DialogFragment. Google recommends that we use DialogFragment instead of simple dialog with Activity.showDialog. DialogFragment will automatically manage life cycle for us.

1. Create class for date picker dialog fragment

a. define a public default constructor otherwise it will crash on screen rotation.
b. Define the onCreateDialog() method to return an instance of DatePickerDialog
c. pass OnDateSetListener to the constructor of DatePickerDialog.

public class DatePickerDialogFragment extends DialogFragment {

    private DatePickerDialog.OnDateSetListener mDateSetListener;

    public DatePickerDialogFragment() {

    }

    DatePickerDialogFragment(OnDateSetListener dateSetListener) {
        mDateSetListener = dateSetListener;
    }

    @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
        DatePickerDialog dpd = new DatePickerDialog(getActivity(), mDateSetListener, year, month,
                day);
        // dpd.getDatePicker().setCalendarViewShown(true);
        // dpd.getDatePicker().setSpinnersShown(false);

        return dpd;
    }

}

2. For activating date picker and for displaying selected date we will use button that will look more like spinner with the help of spinner style.

    

3. use current date as default label of button

  selectDate = (Button) findViewById(R.id.select_date);
  setDate(System.currentTimeMillis());

4. show date picker on click of select_date button.

selectDate.setOnClickListener(datePickerListener);
 OnClickListener datePickerListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerDialogFragment(callback);
        newFragment.show(getSupportFragmentManager(), "datePicker");

        }
    };

5. Define the callback of DatePickerDialogFragment

 OnDateSetListener callback = new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar c = Calendar.getInstance();
            c.set(year, monthOfYear, dayOfMonth);
            setDate(c.getTime().getTime());

        }
    };

6. setDate method for date formating.

  private void setDate(long millisecond){
        int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR
                | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_MONTH
                | DateUtils.FORMAT_ABBREV_WEEKDAY;
        String dateString = DateUtils.formatDateTime(EditContactActivity.this,millisecond, flags);
        selectDate.setText(dateString);

    }

7. We can also use CalendarView for date picker.

        DatePickerDialog dpd = new DatePickerDialog(getActivity(), mDateSetListener, year, month,
                day);
        dpd.getDatePicker().setCalendarViewShown(true);
        dpd.getDatePicker().setSpinnersShown(false);

Incoming search terms:


Viewing latest article 8
Browse Latest Browse All 10

Trending Articles