I have the following code:
public void startDatePickerDialog(View v) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setType("start");
newFragment.show(getSupportFragmentManager(), "startDatePicker");
}
with DatePickerFragment
private String type = "none";
@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
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(getActivity(), "works?", Toast.LENGTH_LONG).show();
if (type.equals("start")) {
EditText e = (EditText) getActivity().findViewById(
R.id.date_of_task);
e.setText("");
} else {
EditText e = (EditText) getActivity().findViewById(
R.id.deadlineDate);
e.setText("");
}
super.onCancel(dialog);
}
public void setType(String t) {
type = t;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if (type.equals("start")) {
EditText e = (EditText) getActivity().findViewById(
R.id.date_of_task);
e.setText("" + day + "." + (month + 1) + "." + year);
} else {
EditText e = (EditText) getActivity().findViewById(
R.id.deadlineDate);
e.setText("" + day + "." + (month + 1) + "." + year);
}
}
My Problem is now that onCancel doesn’t work. Therefore I want to add a Button on the DialogFragment which causes the action in onCreate. How can I add a button or how can I solve this problem.
There is a bug with the date/time pickerdialog. it is something like this :
now :
click on OK – the date gets set twice
click cancel – the date gets cleared then set
click back – date gets set once
expected :
click OK – date gets set once
click Cancel – date gets reset
click back – ntng and it retains the state as it was
As a work-around, you could keep a counter that would get set the first time you call the OnDateSetListener and if the counter is > 1 then return and reset counter.