You can apply text filter to your EditText
by using editText.setFilters(filter)
, So the method used will be like the following:
text = (EditText) findViewById(R.id.text);
validate_text(text);
// the method validate_text that forces the user to a specific pattern
protected void validate_text(EditText text) {
InputFilter[] filter = new InputFilter[1];
filter[0] = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (end > start) {
String destText = dest.toString();
String resultingText = destText.substring(0, dstart)
+ source.subSequence(start, end)
+ destText.substring(dend);
if (!resultingText
.matches("^\\d{1,10}(\\.(\\d{1,2})?)?")) {
return "";
}
}
return null;
}
};
text.setFilters(filter);
}
This will force the user to enter "dot" after entering for digits, and will force him to enter only "two digits" after the "dot".