I have an EditText that has an inputType of number. While the user is typing, I want to comma-separate the numbers. Here's a little illustration:
123 would be represented as 123
1234 would be represented as 1,234
12345 would be represented as 12,345
...and so on.
I tried adding a comma with TextWatcher as shown below:
    EditText edittext = findViewById(R.id.cashGiven);
    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) {
            editText.setText(separateWithComma(editText.getText().toString().trim()));
        }
    });
Pasting the separateWithComma() method here would make this question extra lengthy but then, it works: I tested it on Eclipse. I think the addTextChangedListener does not work this way because my app freezes (and then crashes much later) when I do this.
Is there a better way to achieve this? Thanks in anticipation for a positive response.
 
     
     
     
     
    