I have two date buttons. These two buttons contains a String as text.
If someone clicks on the button a datepicker appears. After this datepicker
I want to check if the two buttons contains the same datestring.
If so, I want to let two buttons appear (below these two buttons) with two
timepickers.


The problem is, the method which checks if the two dates are equal, automatically starts at first after clicking the button. So I have to click the button twice to properly check if the dates are equal.
Here’s my code:
First the onClickListeners
startDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DateDialog dateStart = new DateDialog(startDate, mYear, mMonth, mDay);
checkdates();
}
});
endDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DateDialog dateEnd = new DateDialog(endDate, mYear, mMonth, mDay);
checkdates();
}
});
Second, the checkdates method:
private void checkdates() {
String firstdate = startDate.getText().toString();
String seconddate = endDate.getText().toString();
if (firstdate.equals(seconddate)) {
row1.setVisibility(View.VISIBLE);
row2.setVisibility(View.VISIBLE);
} else {
row1.setVisibility(View.INVISIBLE);
row2.setVisibility(View.INVISIBLE);
}
}
And at last, the Class DateDialog which is called in the onClickListener:
public class DateDialog {
private int mMonth;
private int mYear;
private int mDay;
private Button txtButton;
public DateDialog(Button b, int year, int month, int day){
txtButton = b;
onCreateDialog(year, month, day);
}
protected Dialog onCreateDialog(int year, int month, int day) {
DatePickerDialog dialog = new DatePickerDialog(NostradamusActivity2.parentcontext, mDateSetListener, year,
month, day);
dialog.show();
return null;
}
// updates the date we display in the TextView
private void updateDisplay() {
txtButton.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-").append(mMonth + 1).append("-")
.append(mDay).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();
}
};
}
You can define an
interfaceinsideDateDialogclass like this:and declare a field inside
DateDialogclass asand create a
settermethod for it also.and in
updateDisplay()put this call at the end alsoand when you open a dialog from button
onClick()insideActivityuseDateDialogListenerlike this way: