The dot or comma button is disabled when inputType="number" or inputType="number|Decimal" is pressed. It failed to work with android:digits="0123456789.," also.
The EditText contains a textwatcher which format the number. The textwather is as follows:
mEditWithdrawalAmount.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals(current)) {
mEditWithdrawalAmount.removeTextChangedListener(this);
String replaceable = String.format("[%s .\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
String cleanString = s.toString().replaceAll(replaceable, "").replace("R","").replace(",","");
double parsed;
try {
parsed = Double.parseDouble(cleanString);
} catch (NumberFormatException e) {
parsed = 0.00;
}
String formatted = Utils.formatCurrency(parsed);
current = formatted;
mEditWithdrawalAmount.setText(formatted);
mEditWithdrawalAmount.setSelection(formatted.length());
// Do whatever you want with position
mEditWithdrawalAmount.addTextChangedListener(this);
}
}
});
The problem is the edittext must allow numbers with decimal place also.
- Actual result is : R1000 000
- Desired result is R1000 000.00 or R1000 000.40
Hey check this code.
Hope this help.