1

I want to add a mac address to my database via EditText.

Is it possible to add a colon (:) after every second character?

The colon should be displayed directly in the EditText.

EDIT: Tried it. And I think I am on the right way ( your anwers confirm this :P )

        inputMac = (EditText) view.findViewById(R.id.editText_mac);
        inputMac.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 (s.length() == 2 || s.length() == 5 || s.length() == 7 || s.length() == 9 || s.length() == 12 ){
                    inputMac.setText(inputMac.getText() + ":");
               }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

But now after 12 characters I get e.g. 123456789123:::::

Rod Kimble
  • 1,302
  • 3
  • 18
  • 44

4 Answers4

2

I've already answered a similar question, so this is how you can achieve it:

    String mTextValue;
    Character mLastChar = '\0'; // init with empty character
    int mKeyDel;


    myEditText.addTextChangedListener(new TextWatcher() {

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

           boolean flag = true;
           String eachBlock[] = myEditText.getText().toString().split(":");
           for (int i = 0; i < eachBlock.length; i++) {
                if (eachBlock[i].length() > 6) {
                    flag = false;
                }
           }
           if (flag) {

              myEditText.setOnKeyListener(new View.OnKeyListener() {

               @Override
               public boolean onKey(View v, int keyCode, KeyEvent event) {

                      if (keyCode == KeyEvent.KEYCODE_DEL)
                            mKeyDel = 1;
                            return false;
                      }
                });

               if (mKeyDel == 0) {

                  if (((myEditText.getText().length() + 1) % 3) == 0) {
                      myEditText.setText(myEditText.getText() + ":");
                      myEditText.setSelection(myEditText.getText().length());
                  }
                  mTextValue = myEditText.getText().toString();
               } else {
                  mTextValue = myEditText.getText().toString();
                  if (mLastChar.equals(':')) {
                       mTextValue = mTextValue.substring(0, mTextValue.length() - 1);
                       myEditText.setText(mTextValue);
                       myEditText.setSelection(mTextValue.length());
                  }
                  mKeyDel = 0;
               }

          } else {
              myEditText.setText(mTextValue);
          }

       }

       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (s.length()>0) {// save the last char value
                mLastChar = s.charAt(s.length() - 1);
            } else {
                mLastChar = '\0';
            }
       }

       @Override
       public void afterTextChanged(Editable s) {}

  });

PS: It also handle deleting characters.

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66
  • Your for loop is wrong. It will always be <6 because you're check each element in the array. Rather check the eachBlock.length() directly. – Deepak kaku Mar 05 '19 at 01:17
0

I tried I think I found a way wich is not that complicated. (Its not perfect but I think I will make it)

       inputMac.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                macLengthBefore = inputMac.length();
                Log.d("Textlänge BEFORE", macLengthBefore.toString());
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                macLengthAfter = inputMac.length();
                Log.d("Textlänge AFTER", macLengthAfter.toString());

                if (macLengthAfter > macLengthBefore && ((inputMac.getText().length() + 1) % 3 == 0) && inputMac.length() <= 15) {
                    inputMac.setText(inputMac.getText() + ":");
                    inputMac.setSelection(inputMac.getText().length());
                }

            }
        });

Thanks @Rami for modulo query

Rod Kimble
  • 1,302
  • 3
  • 18
  • 44
0

After few trial and errors I was able to write a simple and working code:

mEditText.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

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

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                if ((s.toString().length() < 17) && ((before == 1 && count == 2) || (before == 4
                        && count == 5))) {
                    String string = mEditText.getText().toString();
                    string = string.concat(":");
                    mEditText.setText(string);
                    mEditText.setSelection(string.length());
                }
            }
        });

Below code goes into your xml file:

<EditText
    android:id="@+id/edit_text"
    style="@style/textfield_wh"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLength="17"
    android:digits="abcdefABCDEF0123456789:"
    android:inputType="text" />
Adarsh Rotte
  • 634
  • 3
  • 8
-1

Try this,

editText1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                *****APPLY YOUR LOGIC HERE*****
                }
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
    }
activesince93
  • 1,716
  • 17
  • 37