7

I want to automatically put a "-" after 6 letters in my EditText and I want the user can continue to write after the "-". (I want the user writes : 1234562 and it appears 123456-2 in the EditText).

But I don't know how to do this and if it is possible. If you can help me, thanks

odiiil
  • 276
  • 1
  • 2
  • 9

6 Answers6

12

Add a textwatcher.Then use the following code:

@Override
public void afterTextChanged(Editable text) {     


    if (text.length() == 6) {
        text.append('-');
    }


}

You can also use multiple condition in the if statement like:

 if (text.length() == 3 || text.length() == 6) {
        text.append('-');
    }
kgandroid
  • 5,507
  • 5
  • 39
  • 69
6

you can also delete characters with this:

text.addTextChangedListener(new TextWatcher() {
        int first = 0;
        int second;

        @Override
        public void afterTextChanged(Editable s) {
            second = first;
            first = s.length();

            if(text.length()==6 && first>second){
                text.append("-");
            }
        }
Erald Haka
  • 182
  • 2
  • 10
2
EditText editText = (EditText) findViewById(R.id.search);

editText.addTextChangedListener(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) {
                        // TODO Auto-generated method stub                          
                    }                       
                    @Override
                    public void afterTextChanged(Editable text) {
                        // TODO Auto-generated method stub                          
                          if (text.length() == 6) {
                          text.append('-');
    }  
                    }
                });
JIthin
  • 1,413
  • 1
  • 13
  • 29
2
EditText editText = (EditText) findViewById(R.id.editText1);
editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) { 
        String text = editText.getText().toString(); 
        if(text.length() == 6){
            editText.append("-");
        }
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

    }

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

    }
});
okarakose
  • 3,692
  • 5
  • 24
  • 44
  • String text = editText.getText().toString(); if(text.length() == 6){ editText.append("-"); } should be in inTextChanged(...){} – Uzair May 19 '15 at 10:49
0

I used some code I found here to answer this question, it worked well for me and I really hope this can help someone else. This is the code modified for this particular question:

myEditText.addTextChangedListener(new TextWatcher() {
    int count=0;

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

    @Override
    public void afterTextChanged(Editable s) {

        //Check if the text is 6 characters length, and if the user is writing.
        //If that condition is true then add a "-" to the end of the text and set the cursor to the last position
        //Otherwise if the text is 6 characters length, and if the user is deleting.
        //By removing the "-" you're actually removing the las digit as well, then the the cursor is set to the last position

        if (count <= myEditText.getText().toString().length() && s.length()==6){

            String numberOnProgress = myEditText.getText().toString() + " ";
            myEditText.setText(numberOnProgress);
            int lastPosition = myEditText.getText().length();
            myEditText.setSelection(lastPosition);

        }else if (count >= myEditText.getText().toString().length() && s.length()==6){
            String numberOnProgress = myEditText.getText().toString().substring(0,myEditText.getText().toString().length()-1);
            myEditText.setText(numberOnProgress);
            int pos = myEditText.getText().length();
            myEditText.setSelection(pos);
        }
        //Update the count value at the end
        count = myEditText.getText().toString().length();
    }
});
Ariel
  • 1,059
  • 2
  • 13
  • 22
  • I forgot, I strongly recommend to change input type to "phone", since other types may crash when trying to use " " and "-" characters – Ariel Nov 03 '20 at 21:30
0

Here's my take in Kotlin:

codeInput.addTextChangedListener(object : TextWatcher {
    private var isFormatting = false

    override fun afterTextChanged(text: Editable) {
        if (isFormatting) {
            return
        }

        isFormatting = true

        val formattedString = text
            .replace("[^0-9]".toRegex(), "") // Keep only digits
            .chunked(6) // Split into groups of 6
            .joinToString("-") // Join the groups with hyphens

        text.clear()
        text.append(formattedString)

        isFormatting = false
    }

    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
})
karlingen
  • 13,800
  • 5
  • 43
  • 74