1

I have an Edittext and want to set the EditText so that when the user is inputting the number to be converted, a thousand separator (,) should be added automaticaaly in realtime to the number .but i want do this in "onTextChanged" method not in the "afterTextChanged" method. How can I?

public class NumberTextWatcherForThousand implements TextWatcher {

EditText editText;


public NumberTextWatcherForThousand(EditText editText) {
    this.editText = editText;


}

@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 view) {
String s = null;
try {
    // The comma in the format specifier does the trick
    s = String.format("%,d", Long.parseLong(view.toString()));
    edittext.settext(s);
} catch (NumberFormatException e) {
}

}

mohamad sheikhi
  • 394
  • 5
  • 16
  • 1
    Possible duplicate of [How to Automatically add thousand separators as number is input in EditText](https://stackoverflow.com/questions/12338445/how-to-automatically-add-thousand-separators-as-number-is-input-in-edittext) – Android Feb 14 '19 at 08:58
  • 1
    Possible duplicate of [How to comma-separate numbers in EditText](https://stackoverflow.com/questions/54728098/how-to-comma-separate-numbers-in-edittext) – Taslim Oseni Feb 18 '19 at 06:57

2 Answers2

1

Try this code:

 et.addTextChangedListener(new TextWatcher() {

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

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                et.removeTextChangedListener(this);

                try {
                    String givenstring = s.toString();
                    Long longval;
                    if (givenstring.contains(",")) {
                        givenstring = givenstring.replaceAll(",", "");
                    }
                    longval = Long.parseLong(givenstring);
                    DecimalFormat formatter = new DecimalFormat("#,###,###");
                    String formattedString = formatter.format(longval);
                    et.setText(formattedString);
                    et.setSelection(et.getText().length());
                    // to place the cursor at the end of text
                } catch (NumberFormatException nfe) {
                    nfe.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                et.addTextChangedListener(this);

            }
        });
Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
1

I use a TextWatcher to trig on every change in EditText, and use this code to separate currency sections and then set to EditText after every character changes:

public static String formatCurrencyDigit(long amount) {
    return String.format("%,d%s %s", amount, "", "");
}
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52