Yes, itβs entirely possible, even you donβt need to manage the "$" and decimal separately.
The following is just an EditText controlled by a TextWatcher :
final EditText editText = (EditText) findViewById(R.id.edt); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editText == null) return; String inputString = editable.toString(); editText.removeTextChangedListener(this); String cleanString = inputString.toString().replaceAll("[$,.]", ""); BigDecimal bigDecimal = new BigDecimal(cleanString).setScale(2, BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100), BigDecimal.ROUND_FLOOR); String converted = NumberFormat.getCurrencyInstance().format(bigDecimal); editText.setText(converted); editText.setSelection(converted.length()); editText.addTextChangedListener(this); } });
And if you need to know how EditText should be created in xml , do the following:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edt" android:inputType="numberDecimal" android:textDirection="anyRtl" android:gravity="right"/>
This is an old question, but maybe a better answer :)
source share