0

I have a few EditText that are dynamically generated. I want to change the focus to the next EditText once the required length is reached. i have put all the Edit Texts on an ArrayList and am trying to add an event to shift the focus to next one but nothing seems to be working. i cannot call the editText field by id because they are dynamically generated.

    final ArrayList<EditText> myEt = new ArrayList<EditText>();

    for(int a = 0; a < g2.getChildCount(); a++){//g2 is parent layout

        if(g2.getChildAt(a) instanceof EditText);
        myEt.add((EditText) g2.getChildAt(a));

    }

    for(int b =0; b>myEt.size()-1; ++b){
         final EditText e;
         final EditText e1;
        e = (EditText) myEt.get(b);
        e1 = (EditText) myEt.get(b+1);
        e.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(e.getText().toString().length() > 4){
                    e.clearFocus();
                    e1.requestFocus();
                }


                return false;
            }
        });

    }
Suman Palikhe
  • 357
  • 1
  • 4
  • 16

1 Answers1

0

Updated

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
       if(s.length()==Your required length)
           {
               input2.requestFocus();
           }

            // TODO Auto-generated method stub
        }

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

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            if(s.length()==Your required length)
           {
               input2.requestFocus();
           }
        }
    });
Gopal Singh
  • 1,133
  • 1
  • 9
  • 25