I am working on an android project and I am using the android:nextFocusButton
so the user can press the next button on the soft keyboard to move through EditText without needing to tap each edit text to change the focus.
In the layout I am asking for the users first name and last name, in separate EditText within a Linear Layout which is in horizontal
Then on the next line, in another linear layout, I then ask for the company name. Below is a screenshot to show what I mean.
What I am trying to do, is in the first name edit text, the user can press next, which should then switch focus to the last name edit text, they then press next again and it moves to the company edit text.
Instead what is happening, is the first name has focus, the user presses the next button, and it goes to the company edit text instead of the last name.
Below is a snippet of my XML layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="@+id/createAccount_txtFirstName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/first_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="@+id/createAccount_txtLastName"/>
<EditText android:id="@+id/createAccount_txtLastName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/last_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="@+id/createAccount_txtCompanyName"/>
</LinearLayout>
<EditText android:id="@+id/createAccount_txtCompanyName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/company_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusForward="@+id/createAccount_txtEmail"/>
None of the solutions worked for me except for listening for editor action listener and programmatically setting the focus to the next EditText.
mFirstName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
mLastName.requestFocus();
return true;
}
});
It might be that your XML is ignored by Android focus algorithm. Documentation says following:
In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides with the following XML attributes in the layout file: nextFocusDown, nextFocusLeft, nextFocusRight, and nextFocusUp.
I had similar problem and i solved it by using nextFocusDown.
More on official documentation
just place this inside your first edittext. Also replace nextFocusForward with nextFocusDown
<EditText android:id="@+id/createAccount_txtFirstName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/first_name"
android:singleLine="true"
android:inputType="textCapSentences"
android:nextFocusDown="@+id/createAccount_txtLastName">
<requestFocus />
</EditText>
It seems to be bugged when running this code on the emulator. However, it works on phones
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With