TextView mEtPrice = (TextView) findViewById(R.id.txt_price);
mEtPrice.addTextChangedListener(new TextWatcher() {
String strAmount = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(strAmount)) {
strAmount = makeStringComma( s.toString().replace(",","") );
mEtPrice.setText(strAmount);
Editable e = (Editable) mEtPrice.getText();
Selection.setSelection(e, strAmount.length());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
public String makeStringComma(String str) {
if (str.length() == 0)
return "";
long value = Long.parseLong(str);
DecimalFormat format = new DecimalFormat("###,###");
return format.format(value);
}