Edittext jumps to next edittexts after reaching the maximum edittext length

My question is very similar to this post: Edittext jumps to next edittext after reaching the maximum edittext length

But, whatever I try to do, I'm not able to jump third edittext as shown in the pic below. I write 10 to the first edittext and it automatically moves to the second edittext but when I write 12, it doesn't move to third edittext. I tried a couple of things but couldn't handle it. I need a simple solution for this problem up to 10-15 edittexts. Can anybody help me about syntax? Thanks.

Here is my activity main xml

<EditText
    android:id="@+id/edittext1"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:maxLength="2"
    android:hint="1"
    android:layout_weight="1"/>
<EditText
    android:id="@+id/edittext2"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:maxLength="2"
    android:digits="0123456789 "
    android:hint="2"
    android:layout_weight="1"/>
<EditText
    android:id="@+id/edittext3"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:maxLength="2"
    android:digits="0123456789 "
    android:hint="3"
    android:layout_weight="1"/>

And here is my main activity

package com.example.serkan.justtry;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements TextWatcher {
    EditText edt1;
    EditText edt2;
    EditText edt3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edt1=(EditText)findViewById(R.id.edittext1);
    edt2=(EditText)findViewById(R.id.edittext2);
    edt3=(EditText)findViewById(R.id.edittext3);

    edt1.addTextChangedListener(this);
}

@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 (edt1.length() == 2) {
        edt2.requestFocus();
    }
}
}

You need to set the listner to edt2 as well

    edt2.addTextChangedListener(this);

just replace

    @Override
    public void afterTextChanged(Editable editable) {

        if(edt1.getText().hashCode() == editable.hashCode()){
            if (edt1.length() == 2) {
                edt2.requestFocus();
            }
        }
        else if( edt2.getText().hashCode() == editable.hashCode()){
            if (edt2.length() == 2) {
                edt3.requestFocus();
            }
        }
    }

On reachin the count you change the focus of the edittext to the next one. Edittext edt1; Edittext edt2: //mount the views to java from xml edt1. In your java code set max length not in your layout.. and check if length is equal to set by you override next button of android keyboard.. – Manish Srivastava Jul 23 '13 at 5:19 Amith it is very easy you have to put addTextChangedListener whenever the length is as max limit set the focus on other Edittext – Yogesh Tatwal Jul 23 '13 at 5:20

Add this as an inner class in your Activity:

private class MyTextWatcher implements TextWatcher {

    private final int maxLength;
    private final EditText next;

    MyTextWatcher(int maxLength, int nextId) {
        this.maxLength = maxLength;
        this.next = (nextId != -1) ? findViewById(nextId) : null;
    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (editable.length() >= maxLength && next != null) {
            next.requestFocus();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        // do nothing
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        // do nothing
    }
}

And then set it up for each EditText like this:

edittext1.addTextChangedListener(new MyTextWatcher(2, edittext1.getNextFocusDownId()));
edittext2.addTextChangedListener(new MyTextWatcher(2, edittext2.getNextFocusDownId()));
edittext3.addTextChangedListener(new MyTextWatcher(2, edittext3.getNextFocusDownId()));

This depends on setting the nextFocusDown attribute in your layout:

<EditText
    android:id="@+id/edittext1"
    android:nextFocusDown="@+id/edittext2"
    .../>

I need to jump to the next edittext after reaching the maximum length . On reachin the count you change the focus of the edittext to the next one Edittext edt1; But I don't know how to implement all this for changes in multiple editTexts. public  EditText max length represents here maximum length of edittext input in characters format. For example app developer can set restrictions to app user so application user cannot enable to enter more then specify characters limit. So here is the complete step by step tutorial for Set maxlength for edittext in android programmatically.

You didin't set the TextChangedListener to the other 2 EditTexts. The listener doens't know when it must change the focus from edt2 to edt3.

Add the listener to the edt2 on your onCreate.

edt2.addTextChangedListener(this);

And update the afterTextChanged

@Override
public void afterTextChanged(Editable editable) {

    if (editable == edt1.getEditableText()) {
        if (edt1.length() == 2) {
            edt2.requestFocus();
        }
    } else if (editable == edt2.getEditableText()) {
        if (edt2.length() == 2) {
            edt3.requestFocus();
        }
    }
}

I didn't test. :)

My question is very similar to this post: Edittext jumps to next edittext after reaching the maximum edittext length. But, whatever I try to do, I'm not  Overview. The EditText is the standard text entry widget in Android apps. If the user needs to enter text into an app, this is the primary way for them to do that. There are many important properties that can be set to customize the behavior of an EditText.

There is no problem in XML file. Just try following code in MainActivity.java

  1. Add TextChangedListener to second edittext

    edt2.addTextChangedListener(this);

  2. Insert following code in onTextChanged method of 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) {
            if(edt1.hasFocus())
        {
            if(edt1.getText().toString().length()==2)     //size as per your requirement
            {
                edt2.requestFocus();
            }
        }
        if(edt2.hasFocus())
        {
            if(edt2.getText().toString().length()==2)     //size as per your requirement
            {
                edt3.requestFocus();
            }
        }
    
    }
    
    @Override
    public void afterTextChanged(Editable editable) {
    
    }
    

I hope this answer will solve your problem :)

I need to focus on the second edit text whenever the maximum limit of characters is reached in first EditText. First of all disable second, third and fourth edittexts. And subscribe on your first edittext TextChange event. Android EditText AutoFocus to next EditText when at maxlength (4) For future readers expecting to COPY/PASTE int MAX_LENGTH = 3 ; txt1 . addTextChangedListener ( new TextWatcher () { @Override public void beforeTextChanged ( CharSequence s , int start , int count , int after ) { } @Override public void onTextChanged ( CharSequence s , int start

When you define an edit text widget, you must specify the R.styleable.​TextView_inputType attribute. For example, for plain text input set  Android EditText entered text limitation can be set through activity_main.xml file using android:maxLength=” ” attribute. With the help of this attribute edittext cannot get more then setting max length characters. So here is the complete step by step tutorial for Set input text max length limit in EditText android xml.

A simple numeric EditText widget for Android that automatically formats input compareTo(max) > 0) { value = max; } DecimalFormat formatter = new Hello, How to restrict edittext to enter 2 digits after decimal point? What I am trying to do is have 3 EditTexts next to each other on the same row. Press 4, it goes to $0. A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities. Styles of edit text EditText Attributes. Following are the important attributes related to EditText control.

You need to a implement a listener for text change event, see example: how to set the min character length for edittext field in android. it is easy to set the maximum no.of characters length by using android:maxLength="*".

Comments
  • remove the else if and have only if check the updated answer