33

According to the question and answer posted How to automatically move to the next edit text in android , I used the code to move to the next edit box.

et1.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start,int before, int count) 
    {
        // TODO Auto-generated method stub
        if(et1.getText().toString().length()==size)     //size as per your requirement
        {
            et2.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
    }

});

The issue I am facing is in case I enter something wrong, then even if the user explicitly request focus , the text box moves to the next one due to the code in the text watcher. Is there anything I can do to prevent this from happening ?

Community
  • 1
  • 1
user1667307
  • 2,041
  • 6
  • 27
  • 32
  • you can use: [KeyCode_Enter to next edittext](http://stackoverflow.com/questions/3019965/keycode-enter-to-next-edittext) – Ketan Patel Nov 03 '15 at 12:01

10 Answers10

38

It might be very late but I have a solution which is much easier to apply. Just add this code to the edittext field in the layout.xml file. Add this to edittext which you want to show a next button instead of a done button. So it will point to the next Edittext field.

android:imeOptions="actionNext"
arshu
  • 11,805
  • 3
  • 23
  • 21
21

If you want to focus next EditText on enter pressed you can use those options at XML code to focus next EditText:

Option 1:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionNext"
    android:singleLine="true"/>

Option 2:

<EditText
    android:id="@+id/et1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:nextFocusForward="@+id/et2"/>

<EditText
    android:id="@+id/et2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"/>
Daniel López
  • 716
  • 7
  • 9
  • Thank you, you saved my day:) – Ehsan Aug 06 '18 at 12:42
  • @DanielLópez Your solution is perfect. Just a heads up to everyone: single line in the edittext is important,otherwise the keyboard option to go forward won't appear. – Ortiz Nov 23 '18 at 21:25
  • if you have only one edit text in recycler view and 10 items of list so how you move edit text next to each other – Adnan haider Apr 01 '21 at 06:23
13

You can check the key pressed for 1st EditText and if it was "Enter" key,then move focus to next EditText.

This may help you: KeyCode_Enter to next edittext

Community
  • 1
  • 1
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
5

You need to validate user input to meet your expectations. If that is met, then only call et2.requestFocus()

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
UVM
  • 9,776
  • 6
  • 41
  • 66
  • The issue is that I myself dont know the range of what is valid and what is not.(eg: all four digit numbers are valid , and if the user needs to change what he entered it is very troublesome) – user1667307 Sep 19 '12 at 04:43
  • what will happen if you do not call et2.requestFocus()? still is it going to your next field? – UVM Sep 19 '12 at 04:46
4

It work for me:

final EditText code1=((EditText)view.findViewById(R.id.code1));
    final EditText code2=((EditText)view.findViewById(R.id.code2));
    final EditText code3=((EditText)view.findViewById(R.id.code3));
    final EditText code4=((EditText)view.findViewById(R.id.code4));



    OnKeyListener key=new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(!((EditText) v).toString().isEmpty())
            v.focusSearch(View.FOCUS_RIGHT).requestFocus();

            return false;
        }
    };




    code1.setOnKeyListener(key);
    code2.setOnKeyListener(key);
    code3.setOnKeyListener(key);
Fabio Guerra
  • 722
  • 6
  • 13
2

You can also check in your if condition that is that right or not..

if(et1.getText().toString().length()==size)     //size as per your requirement
    {
        if(et1.gettext.toString().equals(somethingwhichyouhavecheck))
        {

                   et2.requestFocus();
        }
        else
         {
         }
    }
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • There is absolutely nothing I can check against. It is the user who decides what to enter – user1667307 Sep 19 '12 at 04:35
  • You said that The issue I am facing is in case I enter something wrong. what is wrong and what is write? give example. – Hardik Joshi Sep 19 '12 at 04:37
  • He means to say,if he restricts the size of text in EditText,then if user enters something he doesn't want to enter and wants to correct it once written then that would cause inconvenience. – Hiral Vadodaria Sep 19 '12 at 04:41
  • The user enters some input. (There is no restriction on the input). However if the user feels that he entered a character wrong,he presses on the edit box to change it and the box simply moves to the next one due to the text watcher – user1667307 Sep 19 '12 at 04:41
1

This code works as expected.

etFName.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(etFName.getText().toString().length()==4 && keyCode == KeyEvent.KEYCODE_BACK)
                {
                    typinget1 = true;
                }
                else if(etFName.getText().toString().length()==4 && typinget1==true)     //size as per your requirement
                {
                    typinget1 = false;
                    etLName.requestFocus();

                }
                else if(etFName.getText().toString().length()<=4 && typinget1==false)
                {
                    typinget1 = true;

                }

                return false;
            }
        });
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

this code work for me

      private void managePinView(final ArrayList<MaterialEditText> editTexts) {
    last = editTexts.size();
    last--;
    editTexts.get(0).requestFocus();
    for (int i = 0; i < editTexts.size(); i++) {
      final int finalI = i;

      editTexts.get(finalI).addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
          convertText(editTexts.get(finalI), s, this); // i can use this function to change english number to persian number if do not use comment this
          int textLength = editTexts.get(finalI).getText().length();
          next = finalI + 1;
          previous = finalI - 1;
          if (textLength >= 1) {
            if (next <= last)
              editTexts.get(next).requestFocus();
          } else {
            if (previous >= 0) {
              editTexts.get(previous).requestFocus();
            }
          }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }

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

  private void convertText(MaterialEditText editText, CharSequence charSequence, TextWatcher watcher) {
    String updatedString = G.convertEnNumberToFi("" + editText.getText().toString().substring(0, editText.length()));
    editText.removeTextChangedListener(watcher);
    editText.setText(updatedString);
    editText.setSelection(charSequence.length());
    editText.addTextChangedListener(watcher);
  }
zmag
  • 7,825
  • 12
  • 32
  • 42
abolfazl bazghandi
  • 935
  • 15
  • 29
0

This is my approach for editext in recyclerview or listview or whatever.

edittext.setOnEditorActionListener { textView, actionId, _ ->
     if (actionId == EditorInfo.IME_ACTION_NEXT) {
        var view = textView.focusSearch(View.FOCUS_DOWN)
        while (view != null) {
          if (view is EditText) {
             view.isCursorVisible = true
             return@setOnEditorActionListener view.requestFocus()
          }
          view = view.focusSearch(View.FOCUS_DOWN)
        }
     }
     false
}
nguyencse
  • 1,028
  • 9
  • 17
-3

I have very simple solution for this just put following property in xml edittext control

android:inputType="textPersonName"

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57