Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NextFocus editText in include layout

I create a simple layout containing my label and my editText. This layout is included several time to create a form.

But with this method the "nextFocus" function doesn't work properly and I can't find a solution. I need to give the id of the next editText but because it's inside a include layout, the id is the same as the current editText.

Is their a way to achieve this by using only xml solution? I particulary think of databinding.

This is my layout's :

The include layout :

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>

    <variable
        name="viewModel"
        type="viewModels.StringDataTypeViewModel"/>

    <variable
        name="nextFocus"
        type="Integer"/>

</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:padding="10dp"

    android:orientation="horizontal"
    android:gravity="center_vertical">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        tools:text="Nom de famille : "
        android:text="@{viewModel.label}"

        android:singleLine="true"
        android:ellipsize="end"/>

    <EditText android:id="@+id/value"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:textSize="@dimen/patient_detail_edittext_text_size"

        android:inputType="textPersonName"

        android:ellipsize="end"
        tools:text="Dupont"
        android:text="@={viewModel.value}"
        android:textColor="@android:color/widget_edittext_dark"

        android:clickable="@{viewModel.editable}"
        android:cursorVisible="@{viewModel.editable}"
        android:focusable="@{viewModel.editable}"
        android:focusableInTouchMode="@{viewModel.editable}"

        android:background="@{viewModel.editable ?  @android:drawable/edit_text : @drawable/empty_drawable}"

        android:nextFocusForward="@id/value"/>

</LinearLayout>

The form layout :

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:orientation="vertical"
                android:visibility="@{viewModel.editable ? View.VISIBLE: View.GONE}">

                <include layout="@layout/string_data_type_layout"
                    app:viewModel="@{viewModel.familyNameViewModel}" />

                <include layout="@layout/string_data_type_layout"
                    app:viewModel="@{viewModel.givenNameViewModel}" />

                <include layout="@layout/string_data_type_layout"
                    app:viewModel="@{viewModel.patientCodeViewModel}" />

                <include layout="@layout/string_data_type_layout"
                    app:viewModel="@{viewModel.addressViewModel}" />


            </LinearLayout>
like image 891
MHogge Avatar asked Nov 08 '22 14:11

MHogge


1 Answers

Step 1: Give unique ids to all your include elements

<include android:id="@+id/edit_text_1"
            ... />
<include android:id="@+id/edit_text_2"
            ... />

Step 2: Add these attributes to the root of your included layout xml (LinearLayout in your case)

<LinearLayout
        android:descendantFocusability="afterDescendants"
        android:focusableInTouchMode="true"
        ... />

Step 3: Use the ids defined in step 1 in all your include elements (as per required navigation)

<include android:id="@+id/edit_text_1"
         app:nextFocus="@{@id/edit_text_2}"
         ... />

Cheers!

like image 155
silvermouse Avatar answered Nov 15 '22 12:11

silvermouse