Sardor AKayumov
Sardor AKayumov

Reputation: 79

How to change focus to the next EditText after reaching maxLength?

My app uses registration by phone number. So, I divided phone number inputs for some reasons. Also, I set maxLength for them. Let's imagine that there are two editText and the first one has limits to 2 units. After input of 2 units app should automatically send user(or change focus) to another EditText.

Upvotes: 1

Views: 901

Answers (2)

SebastienRieu
SebastienRieu

Reputation: 1512

in java:

editText1.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(editable.toString().length() == 2) {
                    editText2.requestFocus()
                }
            }
        });

in kotlin:

editText1.doOnTextChanged { text, start, count, after ->
            if(count == 2) {
                editText2.requestFocus()
            }
        }

Upvotes: 1

Sagar gujarati
Sagar gujarati

Reputation: 152

Try this :

edittext1.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start,int before, int count) 
    {
        // TODO Auto-generated method stub
        if(edittext1.getText().toString().length()==size)     //size is your limit
        {
            edittext2.requestFocus();
        }
    }
    public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

    }

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

});

Upvotes: 1

Related Questions