1

I have a date picker and I want to interchange the set and cancel buttons.

when i tried, both buttons are functioning for set only.

How to avoid this and inter change buttons.

here is my code.

@Override
    protected Dialog onCreateDialog(int id) {
        Calendar c = Calendar.getInstance();
        int cyear = c.get(Calendar.YEAR);
        int cmonth = c.get(Calendar.MONTH);
        int cday = c.get(Calendar.DAY_OF_MONTH);
        switch (id) {
        case DATE_DIALOG_ID:
            final DatePickerDialog datePicker = new DatePickerDialog(this,
                    mDateSetListener, cyear, cmonth, cday);
            datePicker.setButton(DatePickerDialog.BUTTON_NEGATIVE,
                    this.getString(R.string.set), datePicker);
            datePicker.setButton(DatePickerDialog.BUTTON_POSITIVE,
                    this.getString(R.string.cancel), datePicker);           
            return datePicker;
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            selectedDay = Integer.parseInt(String.valueOf(dayOfMonth));
            selectedMonth = Integer.parseInt(String.valueOf(monthOfYear + 1));
            selectedYear = Integer.parseInt(String.valueOf(year));
            String date_selected = String.valueOf(monthOfYear + 1) + " /"
                    + String.valueOf(dayOfMonth) + " /" + String.valueOf(year);

            textView.setText(date_selected);

        }
    };

Thanks in advance...!

1 Answer 1

2

The third argument to the setButton() method is the listener (DialogInterface.OnClickListener) that will handle the button press. If you pass in the DatePickerDialog as this argument, pressing the button will call the listener attached when the Dialog is initialized, in this case, mDateSetListener. Both buttons are calling your "set" function because you pass datePicker as the listener to both buttons. Try creating a new DialogInterface.OnclickListener that calls datePicker.dismiss() and use that as the third argument when you set the positive button.

1
  • Thank you..! I created on dummy datepickerdialog for netative button, its working as intended.
    – Noby
    Apr 13, 2012 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.