Android: nextFocusForward goes different than EditText

I am working on an Android project and use android:nextFocusButton so that the user can press the next button on the soft keyboard to navigate the EditText without having to click on each edit text to change focus.

In the layout I ask the name of the user in a separate text EditText in a linear layout that is in the horizontal plane

Then on the next line, in another linear diagram, I ask for the name of the company. Below is a screenshot to show what I mean.

Basic screen layout

What I'm trying to do is in the text editor of the name, the user can click next, after which you should switch the focus to the text for editing the surname, then click the next text again, and he will go to the text for editing the company.

Instead of what happens, the first name has focus, the user clicks the next button, and it goes to the text of the company editing 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"/> 
+9
source share
3 answers

just place it 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 listening when this code is run on the emulator. However it works on phones

+8
source

Your XML may be ignored by the Android focus algorithm. The documentation reads as follows:

In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide an explicit override of the following XML attributes in the file layout: nextFocusDown, nextFocusLeft, nextFocusRight, and nextFocusUp.

I had a similar problem and solved it using nextFocusDown .

More on official documentation

+5
source

None of the solutions worked for me, except for listening to the listener of the editor's action and programmatically setting the focus for the next EditText.

  mFirstName.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { mLastName.requestFocus(); return true; } }); 
+2
source

Source: https://habr.com/ru/post/1204988/


All Articles