When i click date button get date picker dialog and select date, that date will stored in edit text. this time i will perform validation for date. means the date is less than current date it will display alert. This time i got alert but two alerts are displaying. How to avoid two alerts displaying?
private String setdate() {
int fYear, fMonth, fDay;
final Calendar c = Calendar.getInstance();
fYear = c.get(Calendar.YEAR);
fMonth = c.get(Calendar.MONTH);
fDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(RemindMeDetails.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
try{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
//for current date
String cdate = formatter.format(new Date());
Date Cdate = formatter.parse(cdate);
//this date id for selected date
String date=(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year).toString();
Date dateD = formatter.parse(date);
if (Cdate.compareTo(dateD)<=0){
onetime_edit_date.setText(date);
}
else{
showmessage("Alert", "date is not lower than current Date");
}
}catch (ParseException e1){
e1.printStackTrace();
}
}
}, fYear, fMonth, fDay );
dpd.show();
return date;
}
You have to write the validation in showmessage() method. If you showing the alert dialog in the method then check it is already showing or not...like that you can handle it. Otherwise use boolean variable.
EDIT
try like this
AlertDialog alertDialog;
void showmessage(String string, String string2) {
if (alertDialog == null || !alertDialog.isShowing()) {
Builder builder;
builder = new AlertDialog.Builder(this);
builder.setTitle(string);
builder.setMessage(string2);
builder.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog = builder.create();
alertDialog.show();
}
}