I have a FromDate EditText
to which I have added an image. Now when I click the image a Datepicker
is shown. Now when I choose some date and click on Set Button
, the date is set in the EditText
. Problem is that, when I click Cancel Button
, the date picker is gone and the prev activity does not gain focus until I press Back! No touch event works on the screen.
Please help...
Asked
Active
Viewed 9,616 times
1

Abhilasha
- 929
- 1
- 17
- 37
2 Answers
7
I don't believe this could be the best way to do it...but some how the following code works; got some guidance from this link https://stackoverflow.com/a/4981308/840520
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();
onBackPressed();
}
}
});
Since I have a project delivery, this works for me now.However, I'm still looking for a decent way of handling this. Does android platform doesn't handle the cancel button as it does for other dialogs?
1
I have implemented date picker with cancel event handling and with work with EditText.. May be it help someone who wants to clear text on cancel event..
private EditText dob;
static final int DATE_DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.browse);
dob = (EditText) findViewById(R.id.txtDob);
/* For DOB EditText */
dob.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (v == dob)
showDialog(DATE_DIALOG_ID);
return false;
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
final int cyear, cmonth, cday;
if (dob.getText().equals("")) {
Calendar c = Calendar.getInstance();
cyear = c.get(Calendar.YEAR);
cmonth = c.get(Calendar.MONTH);
cday = c.get(Calendar.DAY_OF_MONTH);
} else {
String[] datestr = dob.getText().toString().split("-");
cyear = Integer.parseInt(datestr[0]);
cmonth = Integer.parseInt(datestr[1]);
cday = Integer.parseInt(datestr[2]);
}
switch (id) {
case DATE_DIALOG_ID:
// return new DatePickerDialog(this, mDateSetListener, cyear,
// cmonth,cday);
DatePickerDialog dateDlg = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
String date_selected = String.valueOf(year)
+ "-"
+ ((String.valueOf(monthOfYear + 1)
.length() == 1 ? "0"
+ String.valueOf(monthOfYear + 1)
: String.valueOf(monthOfYear + 1)))
+ "-"
+ ((String.valueOf(dayOfMonth).length() == 1 ? "0"
+ String.valueOf(dayOfMonth)
: String.valueOf(dayOfMonth)));
dob.setText(date_selected);
}
}, cyear, cmonth, cday);
dateDlg.setTitle(getString(R.string.dob));
dateDlg.setCancelable(false);
dateDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dob.setText("");
dialog.dismiss();
}
});
return dateDlg;
}
return null;
}

Bhavin Chauhan
- 1,950
- 1
- 26
- 47