Search code examples
androidincludeandroid-databinding

How to configure an <include> layout in xml


I have included a layout with an EditText into my host-layout. The imeOptions of the EditText in the include-layout should be configurable from my host-layout. Therefore I added a variable and the hook in the include-layout.

My problem is that in the host-layout I want to set the actual flag as value like this

app:customImeOptions="@{actionNext}"

By doing so I get the error "Cannot find identifier 'actionNext'". The only thing that works is setting the integer value.

app:customImeOptions="@{0x00000006}"

The Include-Layout

<data>
    ...
    
    <variable
        name="customImeOptions"
        type="Int"/>

</data>

<ConstraintLayout
    ...
    ...>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="@{customImeOptions}/>

</ConstraintLayout>

The Host Layout <ConstraintLayout ... ...>

    <include
        layout="@layout/include_layout"
        app:customImeOptions="@{0x00000006}" />

Solution

  • To set the flag in the host layout one must import the corresponding class in the host-layout. In this case add this import in the data section of the host-layout

    <data>
    
        <import type="android.view.inputmethod.EditorInfo" />
    
    </data>
    

    Now that the host-layout knows this class it is possible to set the flag instead of the int-value like this

    <include
            layout="@layout/include_layout"
            app:customImeOptions="@{EditorInfo.IME_ACTION_NEXT}" />