You could attach a TextWatcher
to the EditText
(like you said), but then you could implement an if/else statement to check the values in the EditText
. Here is an example of what you could do:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(String.valueOf(s).contains(",")) {
String editTextContent = String.valueOf(editText.getText());
editText.setText(editTextContent.replace(",", ""));
Toast.makeText(getApplicationContext(), "Enter a valid input", Toast.LENGTH_SHORT).show();
} else {
// User entered something valid
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
This will make it so that the user cannot enter a comma. Hope it helps!