Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:nextFocusForward is going to the wrong EditText

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.

Basic Screen Layout

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"/>
like image 926
Boardy Avatar asked Oct 18 '14 20:10

Boardy


3 Answers

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;
        }
    });
like image 152
Lahiru Chandima Avatar answered Oct 23 '22 10:10

Lahiru Chandima


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

like image 45
dafilipaj Avatar answered Oct 23 '22 09:10

dafilipaj


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

like image 8
Danial Hussain Avatar answered Oct 23 '22 08:10

Danial Hussain