Android EditText AddTextChangeListener Currency Format

Through SO I put together the following EditText addTextChangedListener event handler, it works great except for a little improvement I need. When I type, it fills in the numbers from right to left. I would prefer it to fill from left to right. Right now if I enter 50 it is $ 0.50, when I enter 50 I want it to format as $ 50.00

Below is the xml declaration and the Java code, please someone can point me to what I need to change to get the desired result.

enter image description here

XML declaration

    <EditText
        android:id="@+id/valueEditText"
        android:layout_row="1"
        android:hint="@string/hint_value_to_add"
        android:imeOptions="actionNext"
        android:inputType="numberDecimal"
        style="@style/EnrollEditViewStyle" />
    <requestFocus />

      

Java code

      inputValue = (EditText) findViewById(R.id.valueEditText);
        inputValue.addTextChangedListener(new TextWatcher() {
            private String current = "";
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals(current)) {
                    inputValue.removeTextChangedListener(this);

                    String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
                    String cleanString = s.toString().replaceAll(replaceable, "");

                    double parsed;
                    try {
                        parsed = Double.parseDouble(cleanString);
                    } catch (NumberFormatException e) {
                        parsed = 0.00;
                    }
                    String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

                    current = formatted;
                    inputValue.setText(formatted);
                    inputValue.setSelection(formatted.length());
                    inputValue.addTextChangedListener(this);
                }
            }
        });

      

+3


source to share


1 answer


In this SO question EditText format for currency with integers I added setMaximumFractionDigits to 0 like so



@Override
    public void afterTextChanged(Editable s) {
        if (!s.toString().equals(current)) {
            inputValue.removeTextChangedListener(this);

        String replaceable = String.format("[%s,.\\s]",   NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
        String cleanString = s.toString().replaceAll(replaceable, "");

        double parsed;
        try {
            parsed = Double.parseDouble(cleanString);
        } catch (NumberFormatException e) {
            parsed = 0.00;
        }
        NumberFormat formatter = NumberFormat.getCurrencyInstance();
        formatter.setMaximumFractionDigits(0);
        String formatted = formatter.format((parsed));

        current = formatted;
        inputValue.setText(formatted);
        inputValue.setSelection(formatted.length());
        inputValue.addTextChangedListener(this);
    }

      

+3


source







All Articles