user1667307
user1667307

Reputation: 2071

Moving to the next EditText in android

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 ?

Upvotes: 34

Views: 67784

Answers (10)

nguyencse
nguyencse

Reputation: 1041

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
}

Upvotes: 0

abolfazl bazghandi
abolfazl bazghandi

Reputation: 995

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);
  }

Upvotes: 0

UVM
UVM

Reputation: 9914

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

Upvotes: 5

Daniel L&#243;pez
Daniel L&#243;pez

Reputation: 716

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"/>

Upvotes: 21

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21097

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

android:inputType="textPersonName"

Upvotes: -3

arshu
arshu

Reputation: 11915

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"

Upvotes: 40

Fabio Guerra
Fabio Guerra

Reputation: 722

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);

Upvotes: 4

MKJParekh
MKJParekh

Reputation: 34301

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;
            }
        });

Upvotes: 1

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

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

Upvotes: 13

Hardik Joshi
Hardik Joshi

Reputation: 9507

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
         {
         }
    }

Upvotes: 2

Related Questions