Search code examples
javaandroidregexandroid-edittextandroid-textwatcher

Format TextWatcher android


My FourDigitCardFormatWatcher add a space after every 4 numbers. I want to change the FourDigitCardFormatWatch to the following format 55555 5555 555 55.

How can i determine after 5 numbers add a space, then after 4 digits add a space and after 3 digits add a space.

Actual result: 4444 4444 4444

Expected result: 44444 4444 444


Solution

  • Edit the class like this..

    public class FourDigitCardFormatWatcher implements TextWatcher {
    
    // Change this to what you want... ' ', '-' etc..
    private final String char = " ";
    EditText et_filed;
    
    
    public FourDigitCardFormatWatcher(EditText et_filed){
        this.et_filed = et_filed;
    }
    
    @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 s) {
        String initial = s.toString();
        // remove all non-digits characters
        String processed = initial.replaceAll("\\D", "");
    
        // insert a space after all groups of 4 digits that are followed by another digit
        processed = processed.replaceAll("(\\d{5})(\\d{4})(\\d{3})(?=\\d)(?=\\d)(?=\\d)", "$1 $2 $3 ");
    
        //Remove the listener
        et_filed.removeTextChangedListener(this);
    
        //Assign processed text
        et_filed.setText(processed);
    
        try {
            et_filed.setSelection(processed.length());
        } catch (Exception e) {
            // TODO: handle exception
        }
    
        //Give back the listener
        et_filed.addTextChangedListener(this);
    }
    }
    

    To add the listener

    editText1.addTextChangedListener(new FourDigitCardFormatWatcher(editText1));