Search code examples
androidandroid-edittextfocusandroid-softkeyboard

SetNextFocusDownId not behaving correctly


I have been struggling with my Android application lately. In my application I have a TableLayout that I dinamically populate within the app code, including an EditText in each cell and the soft keyboard's "Next" button was not behaving correctly (jumping to the next row before getting to the other cells in the same row). So I decided to manually override the focus change by assigning IDs to the EditTexts and then using the setNextFocusDownId method to point to the next EditText. But this didn't work either.

I'll explain the situation. My app has a TableLayout with three columns, this way:

EditText(1)      EditText(5)       EditText(10)
EditText(50)     EditText(100)     EditText(160)
EditText(500)    EditText(1000)    EditText(1600)
EditText(2500)   EditText(3000)    EditText(3500)
EditText(3800)   EditText(4000)    EditText(4500)

Where the numbers inside the brackets are the IDs of the EditText. While populating this TableLayout I save the list of EditTexts in an ArrayList named focusList so I can assign the NextFocus Id later. After populating the table, I call this method:

private void manageFocusList() {
    for (int i=0; i<focusList.size()-1; i++){
        focusList.get(i).setImeOptions(0x05);
        focusList.get(i).setNextFocusDownId(focusList.get(i+1).getId());
    }

    focusList.get(focusList.size()-1).setImeOptions(0x06);

}

The list of EditText is properly sorted in the ArrayList and the links between the EditTexts are succesfully done.
To test this I put some print traces when I press the Next button in the soft keyboard. This is what I get:

01-07 19:34:32.121: I/System.out(27287): My ID is: 1
01-07 19:34:32.121: I/System.out(27287): NextFocusDown: 5

01-07 19:34:32.521: I/System.out(27287): My ID is: 5
01-07 19:34:32.521: I/System.out(27287): NextFocusDown: 10

01-07 19:36:50.146: I/System.out(27287): My ID is: 10
01-07 19:36:50.146: I/System.out(27287): NextFocusDown: 50

and so on...

But in the application, it jumps this way:

1->5
5->50
50->100
100->160
160->1600
1600->1000
1000->500
500->2500

and from now on it continues doing it right

2500->3000
3000->3500
3500->3800
3800->4000
4000->4500

I tested both in a Samsung Galaxy S3 and in the emulator and the behaviour is exactly the same.
I hope you can help me!
Thank you for your time!


Solution

  • OK, after some research, I finally found the solution, yet I don't know what was causing this behaviour.

    I had to manually request the focus for the next edittext using the onEditorAction event listener. The only part of my code I had to change was the manageFocusList() method:

    private void manageFocusList() {
        for (int i = 0; i < focusList.size() - 1; i++) {
            focusList.get(i).setImeOptions(5);
            final int nextId = focusList.get(i + 1).getId();
            focusList.get(i).setOnEditorActionListener(
                    new OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId,
                                KeyEvent event) {
                            if (actionId == EditorInfo.IME_ACTION_NEXT){
                                ((EditText) findViewById(nextId)).requestFocus();
                                return true;
                            }                               
                            return false;
                        }
                    });
        }
        focusList.get(focusList.size() - 1).setImeOptions(6);
    }