Tuesday, October 26, 2010

Soft keyboard next button not working?

I recently ran across a problem with the soft keyboard's next button not advancing focus to the next EditText. In my case I implemented a RelativeLayout in which the EditText boxes were side-by-side, instead of the usual stacked approach. It seems the Next button always tries to focus the next line, not taking into account additional objects on the same line (at least in my particular layout). Luckily, Android provides a method for overriding this behavior with custom instructions.

You have to be careful about setting android:nextFocusDown in your XML layout because errors will be thrown if the objects you are referencing haven't been created yet. I ran into that situation, so I set the references programmatically.

In this instance, I have three side-by-side-by-side EditText objects inside of a RelativeLayout: et1, et2 and et3. Pressing the next button in et1 should focus et2. Pressing the next button in et2 should focus et3. Finally, a "Done" button is in place for et3 so we can stop there. The code to accomplish this is listed below:
// Get references to layout objects
EditText et1=(EditText)findViewById(R.id.et1);
EditText et2=(EditText)findViewById(R.id.et2);
EditText et3=(EditText)findViewById(R.id.et3);

// Set keyboard next buttons