Can you set "tab order" in XML Layout?
AndroidAndroid Problem Overview
I've got users with tablets that have a tab key on their soft keyboard. I am using a table view and have 3 EditText Fields in one row with EditText fields below the current row.
When the user hits tab it takes them to the next field below, not the next field to the right.
Is there a way in the layout to set the tab order or is it only done programatically?
If it can only be done in Java, how exactly is it done?
Thanks for the tip on NextFocusRight but it didn' seem to work (or I did something wrong)
Here is the code I used. I have to add the imeoptions to get the "next" button to show up in my emulator. Do you see anything wrong with the way I did this?
<EditText
android:id="@+id/bikeHHT"
android:layout_width="50dip"
android:layout_height="40dip"
android:textSize="14px"
android:maxLength="2"
android:nextFocusLeft="@+id/bikeMMT"
android:imeOptions="actionNext"
android:layout_column="1"/>
<EditText
android:id="@+id/bikeMMT"
android:layout_width="50dip"
android:layout_height="40dip"
android:textSize="14px"
android:nextFocusRight="@+id/bikeSST"
android:imeOptions="actionNext"
android:maxLength="2"/>
Android Solutions
Solution 1 - Android
Replace android:nextFocusLeft
by android:nextFocusDown
. The "next" soft button or the TAB key is looking for the next "down" focus and not left or right.
Solution 2 - Android
I also notice that in your reference:
android:nextFocusRight="@+id/bikeSST"
The "+" character is required if you have not yet defined that id, if perhaps the element you reference is lower in the .xml file.
Otherwise you if it's already defined above (or included) then you can omit the "+" char:
android:nextFocusRight="@id/bikeSST"
An advantage of not using the "+" when you are referencing an ID that should already exist, is that the IDE and Lint checks can detect an error case: That you are referring to an ID that in fact does not exist.
If you include the "+", then you are creating it if doesn't exist and this check cannot be performed.
Solution 3 - Android
For me android:nextFocusUp
, android:nextFocusRight
, android:nextFocusDown
and android:nextFocusLeft
were not working, but android:nextFocusForward
did work.
Solution 4 - Android
Did you check android:imeOptions="actionNext"
attribute for the EditText ?
It replaces the name of the enter key with "Next" in the virtual keyboard and by clicking on it - focus jumps to the next field.
In your layout, you would do things like this
<EditText
...
android:text="Field 1"
android:imeOptions="actionNext" />
<EditText
...
android:text="Field 2"
android:imeOptions="actionNext" />
And in Java
TextView field2 = (TextView) field1.focusSearch(View.FOCUS_RIGHT);
field2.requestFocus();
It's up to you to decide which field will request focus next.
Solution 5 - Android
There is a better way. There is an XML argument that you can put in your layout that automatically moves the cursor through each EditText
as the user completes them. This allows for much easier form implementation.
Problem is, for the damn life remember what hell it is...
Be sure to check out the android:nextFocus* xml properties. Normal form action is to go down. But perhaps there's a button to the right that you'd like to receive next focus (like hitting enter or similar).
http://developer.android.com/reference/android/view/View.html#attr_android:nextFocusDown
There's also some programatic options available:
http://developer.android.com/reference/android/view/View.html#setNextFocusDownId(int)
Solution 6 - Android
You might be looking for the attributes android:nextFocusRight, android:nextFocusLeft, android:nextFocusUp, and android:nextFocusDown. If the default behavior of the tab button is moving focus down to the second row, then I would think you could set android:nextFocusDown to the id of the view that you want to get focus next. Then when the user presses tab it wold move focus to whatever view you specify, even though in this case it happens to be to the right of the current view, and not below it.
Solution 7 - Android
Replace android:nextFocusLeft
with android:nextFocusForward
.