In my Android app I've got an EditText in which the user can enter an amount. When the EditText loses focus I format the entered amount to a currency Locale using NumberFormat, and when the user clicks the edittext, it takes the original string again and enters that (code here).
This works fine, but it doesn't really look pretty while the user enters the amount. Ideally I would like the EditText to also display the following (in order of importance):
- the Currency symbol on the left of the amount.
- Localised thousand separators.
- Decimal mark only if it is entered by the user.
The first problem I run into however, is that when I use a TextWatcher and implement something like the snippet below, I get a (how well suited to this website) StackOverflowException, because it then watches its own change.
@Override
public void afterTextChanged(Editable s) {
amount_field.setText("€" + s.toString());
}
So my question: how do you format an EditText in realtime? All tips are welcome!