I'm adding a listener to an EditText field to appropriately format the number to currency in real time.
loanText.addTextChangedListener(new TextWatcher() {
private String current = "";
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formated = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formated;
loanText.setText(formated);
loanText.setSelection(formated.length());
}
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});
However, when I try to clear the EditView field, my App will crash. In addition, the backspace key no longer functions.