0

What I have:

I have an EditText which accepts decimal input and I setted the default value in the XML to 0.00 (if there is no input yet)

If the user press 1, then the value change to 1. If he needs to enter decimal value then he must press the dot . on the keyboard.

What I want:

I want to auto format the EditText in a decimal format 0.00, so if a user press 1 if becomes 0.01 instead of 1 (Like in the PAYPAL App). If he entered 100, then the value in the EditText must be formatted as 1.00.

I know this can be done by using TextWatcher but I don't know how to achieve this.

End Result Comparison:

WHAT I HAVE

1 = 1

11 = 11

222 = 222

WHAT I NEED

1 = 0.01

11 = 0.11

222 = 2.22

UPDATE:

My actual TextWatcher looks like below just to set the default value to 0.00 if the user deletes all inputs (to prevent error on calculated functions).

private class EditTextListener implements 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 (editText.getText().toString().equals("") || editText.length() < 1){
                editText.setText("0.00");
                editText.post(() -> editText.setSelection(editText.getText().length()));
            } else {

                BigDecimal amount = new BigDecimal(editText.getText().toString());
            }
        }
    }
Félix Maroy
  • 1,389
  • 2
  • 16
  • 21

2 Answers2

0

as you described you need to divide it by 100 with decimal points:

@Override
public void afterTextChanged(Editable s) {
    
    if (editText.getText().toString().equals("") || editText.length() < 1) {
        editText.setText("0.00");
        editText.post(() -> editText.setSelection(editText.getText().length()));
    } else {
        
        double amount = Double.parseDouble(editText.getText().toString());
        editText.setText(String.format("%.2f", (amount / 100.0));
    }
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
  • It converts as I wanted but cannot be setted back as value to the editText. With this exemple I get a ``NumberFormatException for input String`` when the output is (0,05) if user pressed 5. If I convert to the Locale.ENGLISH I get the output (0.05) but the app enter in infinite loop with huge Memory consuming then it shut down. – Félix Maroy Dec 09 '20 at 12:07
0

Try use this code

edtQtdVolume.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) {
                    edtQtdVolume.removeTextChangedListener(this);

                    String cleanString = s.toString().replaceAll("[R$,.]", "");

                    double parsed = Double.parseDouble(cleanString);

                    Locale locale = new Locale("en", "US");
                    NumberFormat nf = NumberFormat.getNumberInstance(locale);
                    DecimalFormat formatter = (DecimalFormat) nf;
                    formatter.applyPattern("#0.00");
                    String formatted = formatter.format((parsed/100));

                    edtQtdVolume.setText(formatted);
                    edtQtdVolume.setSelection(formatted.length());

                    edtQtdVolume.addTextChangedListener(this);

                    lista.get(position).setQtdVolumeInformadoColeta(Double.valueOf(formatted));
                }

                @Override
                public void afterTextChanged(Editable s) {


                }
            });