Search code examples
androidandroid-layout

NextFocusRight doesn't focus on the right, but down


I'm work on an app with multiple edittext. When an edittext take the whole width, the nextfocusdown is working properly. But when there is two edittext at the same height (each edittext takes half of the width), I want to give the focus from the left one to the right one. But when i write nextFocusRight="id/EDIT_RIGHT" in my xml, the focus doesn't goes to the right edittext but goes to edittext under the left one.

enter image description here

    <!-- EDIT LEFT-->
    <EditText
        android:id="@+id/EDIT_LEFT"
        android:layout_width="165dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:hint="@string/edit_left_hint"
        android:nextFocusRight="@id/EDIT_RIGHT"
        tools:ignore="Autofill"/>

    <!-- EDIT RIGHT-->
    <EditText
        android:id="@+id/EDIT_RIGHT"
        android:layout_width="165dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:hint="@string/edit_right_hint"
        android:nextFocusDown="@id/EDIT_DOWN"
        tools:ignore="Autofill"/>

Solution

  • You have to use like this -

    <!-- EDIT LEFT-->
        <EditText
            android:id="@+id/EDIT_LEFT"
            android:layout_width="165dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:hint="@string/edit_left_hint"
            android:imeOptions="actionNext"
            android:nextFocusRight="@+id/EDIT_RIGHT"
            tools:ignore="Autofill"/>
    
        <!-- EDIT RIGHT-->
        <EditText
            android:id="@+id/EDIT_RIGHT"
            android:layout_width="165dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:hint="@string/edit_rgiht_hint"
            android:imeOptions="actionNext"
            android:nextFocusDown="@id/EDIT_DOWN"
            tools:ignore="Autofill"/>
    

    You should use android:nextFocusRight="@+id/EDIT_RIGHT" instead of android:nextFocusRight="@id/EDIT_RIGHT" because you are using this edittext before creating it and use imeOptions.