2

I want to Automatically add thousand separators in EditText while user types using the last solution in this topic.

I've already made a seperate java file as a class as described and pasted the given code. but I have no idea how to modify my activity code in order to achieve the desired effects.

Here's my XML code:

        <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:id="@+id/editText"
        android:layout_weight="1"/>

and my related Activity is like:

public class CostofHearingActivity extends AppCompatActivity {

public void calculate(View view){

// some calculations


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_costof_hearing);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

any help would be appreciated.

Community
  • 1
  • 1
mdehghani
  • 504
  • 1
  • 7
  • 23

1 Answers1

0

Fist of all, where is the reference of your EditText in your activity class?

Second, as the link you mentioned says, you must add a text watcher to your edittext and reset the input text of user.

You can use the code below in your activity:

private EditText editText;
private final TextWatcher watcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void afterTextChanged(Editable view) {
        String s = null;
        try {
            s = String.format("%,d", Long.parseLong(view.toString()));
        } catch (NumberFormatException e) {
        }
        editText.removeTextChangedListener(watcher);
        editText.setText(s);
        editText.addTextChangedListener(watcher);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_costof_hearing);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ...
    editText = (EditText) findViewById(R.id.editText);
    editText .addTextChangedListener(watcher);
}

I haven't checked it myself it might result in changing the place of cursor in EditText. Check it and update me too plz :)

The reason of temporarily removing TextWatcher is that if the watcher remains on EditText, it will get called again when calling editText.setText(s) and create a loop which will result an StackOverflow Exception.

Sina Rezaei
  • 529
  • 3
  • 24
  • thank you sir! I did as you said and the 3rd digit gets divided by comma but there are problems.. first: the numbers get typed in a reverse format. therefore 123 results in 321. second: upon typing the 5th digit the edittext resets. did you consider the separate java class i mentioned at all? @Sina Rezaei – mdehghani Jan 01 '16 at 18:32
  • anyway I got a clue from your answer and managed to use the class myself.. thanks a lot dear friend :) – mdehghani Jan 01 '16 at 19:13
  • you should accept the answer if the answer helped you – Santosh Jan 02 '16 at 06:21