Reputation: 45
When my submit button is clicked, it calls a method which checks if a certain EditText is empty and displays a toast message as an error message accordingly. However, if I rapidly tap on submit, it "queues" a lot of toast messages. How can I prevent this?
Here's my method:
private void checkName() {
if (etName.getText().toString().isEmpty()) {
Toast toast = Toast.makeText(this, "Please enter your name", Toast.LENGTH_LONG);
toast.show();
} else {
submit();
}
}
Upvotes: 4
Views: 2598
Reputation: 6058
What happens is you are creating new toasts each time checkName()
is called, hence they are "queued" by the system and shown one after another. You can try the following to ensure you are just making one toast and simply show it when needed:
Toast mToast;
private void checkName() {
if (etName.getText().toString().isEmpty()) {
if (mToast == null) { // Initialize toast if needed
mToast = Toast.makeText(this, "", Toast.LENGTH_LONG);
}
mToast.setText("Please enter your name"); // Simply set the text of the toast
mToast.show(); // Show it, or just refresh the duration if it's already shown
} else {
submit();
}
}
Upvotes: 6