lionheart
lionheart

Reputation: 437

EditText focus issue

I've got an issue with several EditText components which reside side-by-side (They are children of RelativeLayout).

                <EditText
                android:id="@+id/num1"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:maxLength="1"                    
                android:inputType="number"
                android:textColor="@color/textColor"
                android:gravity="center"
                android:focusableInTouchMode="true"
                android:focusable="true"
                />
            <EditText
                android:id="@+id/num2"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:maxLength="1"
                android:layout_toEndOf="@id/num1"
                android:layout_marginStart="10dp"                    
                android:inputType="number"
                android:textColor="@color/textColor"
                android:gravity="center"
                android:focusableInTouchMode="true"
                android:focusable="true"
                />
            <EditText
                android:id="@+id/num3"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:maxLength="1"
                android:layout_toEndOf="@id/num2"
                android:layout_marginStart="10dp"                    
                android:inputType="number"
                android:textColor="@color/textColor"
                android:gravity="center"
                android:focusableInTouchMode="true"
                android:focusable="true"
                />

Also, following is the relevant code that tries to change focus from num1 component to num2 component, but actually what happens is that the focus goes from num1 to num3:

        final EditText etLastNumber1 = (EditText) findViewById(R.id.num1);
    final EditText etLastNumber2 = (EditText) findViewById(R.id.num2);
        etLastNumber1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (Misc.IsKeyCodeNumber(keyCode)) {
                etLastNumber1.clearFocus();
                etLastNumber2.requestFocus();
                return false;
            }
            return true;
        }
    });

I've tried different variations of the same code, including not to use clearFocus and/or always return true. Also, the parent layout uses descendantFocusability=beforeDescendants and focusableInTouchMode=true.

How to fix this issue?

Upvotes: 0

Views: 186

Answers (2)

lionheart
lionheart

Reputation: 437

OK, after few weeks of developing other aspects in the android app, I've returned to this issue and I've found the way to fix this issue.

The issue is primarily with context to the Key Events that might happen during the input and focused components.

That said, I've added to the OnKeyListener method the highlighted conditions:

                if (event.getAction() != KeyEvent.KEYCODE_SOFT_LEFT && event.getAction() != KeyEvent.KEYCODE_SOFT_RIGHT && Misc.IsKeyCodeNumber(keyCode)) {

            }

Upvotes: 0

Amy
Amy

Reputation: 4032

Try one of them:

android:nextFocusDown="@+id/.."  
android:nextFocusLeft="@+id/.."    
android:nextFocusRight="@+id/.."    
android:nextFocusUp="@+id/.."

Example:

<EditText
                android:id="@+id/num1"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:maxLength="1"                    
                android:inputType="number"
                android:textColor="@color/textColor"
                android:gravity="center"
                android:focusableInTouchMode="true"
                android:focusable="true"
                android:nextFocusDown="@+id/num2"
                />

Upvotes: 2

Related Questions