0

i'm building a simple binary decimal converter and i want the binary strings to be formatted in groups of 4 bits as the user inputs them.

Watching other similar questions led me to manipulate the editable in method "afterTextChanged" but when i run the app and input the 4° digit the app freezes and the RAM monitor starts to go crazy.

Also, my edittext "binary" is set to "numberDecimal". I tried to change that temporarily using something i found Here, but i can't seem to get it working.

Here's the code: (binary is my EditText)

binary.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) {

            if(binary.isFocused()){

               temp = binary.getText().toString().replaceAll("\\s+","");

                  if(temp.length()%4==0){
                     InputFilter[] filters = editable.getFilters(); // save filters
                     editable.setFilters(new InputFilter[] {});     // clear filters
                     editable.insert(editable.length()-1," ");              // edit text
                     editable.setFilters(filters);
                  }

Thank you in advance :D

Community
  • 1
  • 1
Alessandro Sperotti
  • 183
  • 1
  • 1
  • 12

3 Answers3

1

I figured out: the adding space was somehow triggering an infinite loop. i managed to resolve that removing the focus before adding the space and re-adding the focus after that. i also made some change to the if condition, so now i can easily delete my binary number without having the cursor stuck.

 @Override
 public void afterTextChanged(Editable editable) {

        if(binary.isFocused()){

              if(((temp.length()%4 == 0 && temp.length()!=0) && tempSpace.length()<temp.length()) || (tempSpace.isEmpty() && temp.length()%4 == 0)){

                            InputFilter[] filters = editable.getFilters(); // save filters
                            editable.setFilters(new InputFilter[] {});

                           binary.clearFocus();                                // clear filters
                            editable.insert(editable.length()," ");
                            binary.requestFocus();
                                                           // edit text
                            editable.setFilters(filters);
              }

              tempSpace= temp;

Where tempSpace is a temporary variable used to compare the length of the edittext before the user pressed the botton.

Thank you anyway for your kind answers :D

Alessandro Sperotti
  • 183
  • 1
  • 1
  • 12
0

You should implement a TranformationMethod instead. With TextWatcher you have the problem of any changes in afterTextChanged() triggering another change, so you may end up looping infinitely if you aren't careful about how you detect when it's apprpriate to respond to the change.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

This code breaks the string apart and groups into 4 characters before a space is automatically added.

EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ed = (EditText)findViewById(R.id.editText);
    ed.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)
        {
            if (count == 4)
            {
                ed.setText(ed.getText()+" ");
                ed.setSelection(ed.getText().length());
            }
        }
        @Override
        public void afterTextChanged(Editable s) {}
    });

I hope that it helps

Janwilx72
  • 502
  • 4
  • 18