I am making a reminder app in which the user needs to set date and time from date and time pickers. So I did the following:
private TextView mDateDisplay;
private TextView mTimeDisplay;
...
// Set reminder button
setButton = (Button) findViewById(R.id.set_reminder_button);
setButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!validate()) {
onSetDateTimeFailed();
return;
}
// creating new product in background thread
db.addReminder(id, mEditMessageText.getText().toString(), mDateDisplay.getText().toString(), mTimeDisplay.getText().toString());
finish();
}
});
...
// Validation
public boolean validate() {
boolean valid = true;
String date = mDateDisplay.getText().toString();
String time = mTimeDisplay.getText().toString();
if (date.isEmpty() ) {
mDateDisplay.setError("Set date");
valid = false;
} else {
mDateDisplay.setError(null);
}
if (time.isEmpty()) {
mTimeDisplay.setError("Set time");
valid = false;
} else {
mTimeDisplay.setError(null);
}
return valid;
}
public void onSetDateTimeFailed() {
Toast.makeText(getBaseContext(), "Setting date and time failed", Toast.LENGTH_LONG).show();
setButton.setEnabled(true);
}
I get is the error symbols on the textviews (!), but the only toast message I get is the "Setting date and time failed" and not the "Set date" or "Set time".
The other thing I wanted to do is add a reminder message to date and time:
private EditText mEditMessageText;
...
// Validation
public boolean validate() {
boolean valid = true;
String message = mEditMessageText.getText().toString();
if (message.isEmpty() || time.length() < 1 || time.length() > 140) {
mEditMessageText.setError("Enter between 1 and 140 characters");
valid = false;
} else {
mEditMessageText.setError(null);
}
But now I also get the "Enter between 1 and 140 characters" message when there is error in any validated field. For example, if I entered the needed number of letters in editText filed and did not select date or time I get this error message "Enter between 1 and 140 characters" and the error symbol (!) in editText as well.