I needed to format money values entered into an EditText
so I used a TextWatcher
but now I have issues with backspace in soft keyboard.
Normally if you hold the backspace key in the keyboard, it keeps removing characters in EditText
until there is nothing left. After adding the TextWatcher
, you need to manually press the backspace button many times in order to completely get rid of all characters as holding it no longer works.
How do I fix it?
public class NumberTextWatcher implements TextWatcher {
private final EditText et;
public NumberTextWatcher(EditText et) {
this.et = et;
}
@Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
try {
String originalString = s.toString();
long longval;
if (originalString.contains(",")) {
originalString = originalString.replaceAll(",", "");
}
longval = Long.parseLong(originalString);
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
formatter.applyPattern("#,###,###,###");
String formattedString = formatter.format(longval);
//setting text after format to EditText
et.setText(formattedString);
et.setSelection(et.getText().length());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
et.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}