Yesterday looking for skins for a EditText
, I found the following code snippet here on the site:
public static TextWatcher amount(final EditText editText) {
return new TextWatcher() {
DecimalFormat dec = new DecimalFormat("0.00");
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
private String current = "";
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(!s.toString().equals(current)){
editText.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formatted;
editText.setText(formatted);
editText.setSelection(formatted.length());
editText.addTextChangedListener(this);
}
}
};
}
in which one I use like this
final EditText txtMountCashOut = (EditText) findViewById(R.id.txtMountCashOut);
txtMountCashOut.addTextChangedListener(CashOut.amount(txtMountCashOut));
Now what I don't understand is why when I am going to write, a dollar sign appears in front of the 0.00, as in the following image
I would like to be able to put something else, such as USD
the source of the code is in this question
https://stackoverflow.com/questions/5107901/better-way-to-format-currency-input-edittext
Using the instance of
NumberFormat.getCurrencyInstance()
, you can change the symbol using the methodsetDecimalFormatSymbols(simbolFormat)
:What this does is change the symbol formatter from decimalFormatter to US