On my view I have a button which when pressed pops up a DatePickerDialog. The poppedup dialog has a \"Done\" button. When I press that button the selected date is populated
It looks like there is a bug with Jelybean where the Cancel button isn't working(& hence the back button). This is discussed in Jelly Bean DatePickerDialog --- is there a way to cancel?
David Cesarino, who reported the bug and workaround in the above post, posted his solution here and SO.
cavega slightly modified the above solution to allow initialization of the date in the DatePickerDialog to something other than today's date. Code can be found here. I used his solution and got it to work.
public class pickerdate extends Activity {
/** Called when the activity is first created. */
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
}
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
}
The above code worked for me. The dialog has set and cancel button. Set will set the date and cancel will dismiss dialog. Modify the same according to your needs. Clicking back button will also dismiss dialog.
use following method, which is called when user presses Back button. This method is of Activity
.
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
dialog.dismiss();
}
You could try include this in your alertDialog
If you cancel the dialog, mEdit will return as empty.
mEdit.setText("");
AlertDialog alertDialog;
alertDialog.setOnCancelListener(new OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
// TODO Auto-generated method stub
mEdit.setText("");
dialog.dismiss();
}
});
I wrote simple successor of standard DatePickerDialog that works fine for me:
/**
* Enhanced date picker dialog. Main difference from ancestor is that it calls
* OnDateSetListener only when when pressing OK button, and skips event when closing with
* BACK key or by tapping outside a dialog.
*/
public class IBSDatePickerDialog extends DatePickerDialog {
public IBSDatePickerDialog(final Context context, final OnDateSetListener callBack, final int year, final int monthOfYear, final int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
}
public IBSDatePickerDialog(final Context context, final int theme, final OnDateSetListener callBack, final int year, final int monthOfYear, final int dayOfMonth) {
super(context, theme, callBack, year, monthOfYear, dayOfMonth);
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
// Prevent calling onDateSet handler when clicking to dialog buttons other, then "OK"
if (which == DialogInterface.BUTTON_POSITIVE)
super.onClick(dialog, which);
}
@Override
protected void onStop() {
// prevent calling onDateSet handler when stopping dialog for whatever reason (because this includes
// closing by BACK button or by tapping outside dialog, this is exactly what we try to avoid)
//super.onStop();
}
}
Using dialog example (free bonus: adding Cancel button to dialog for even better usability):
public static class datePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
Calendar cal = Calendar.getInstance();
IBSDatePickerDialog dlg = new IBSDatePickerDialog(myActivity, this, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
// Add Cancel button into dialog
dlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", (OnClickListener) null);
return dlg;
}
@Override
public void onDateSet(final DatePicker view, final int year, final int month, final int day) {
// TODO: do whatever you want with date selected
}
}
public void showDatePickerDialog() {
final datePickerFragment f = new datePickerFragment();
f.show(getSupportFragmentManager(), "datePicker");
}
I think you are asking for this
setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}