Search code examples
androidandroid-edittext

Automatically add colon to Edittext


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:::::


Solution

  • 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.