Home > database >  Android: Referencing an XML view from another XML view in the same layout
Android: Referencing an XML view from another XML view in the same layout

Time:07-09

I am trying to reference a view in the xml as such:

<Button
android:id="@ id/button"
android:onClick="@{(v) -> events.userClicked(another_layout_id)}"
.
.
.
android:text="Click me
/>"

where events is a variable and userClicked() is a function that accepts a view and another_layout_id is another View in the xml layout that button is in (though not same hierachy).

According to the top answer here: Pass another view as parameter in data binding, I should be able to reference another_layout_id but I am receiving a "cannot find identifier" error. Any idea why this is?

EDIT: Added XML code:

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

    <data>

        <variable
            name="state"
            type="bindingModel" />

        <variable
            name="events"
            type="DataModel" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    >

     <TextView
      android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="label"/>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@ id/another_layout_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           >

            <androidx.constraintlayout.helper.widget.Flow
                android:id="@ id/flow_layout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:flow_horizontalGap="8dp"
                />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <Button
            android:id="@ id/show_all"
            style="@style/TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{(v) -> 
            events.userClicked(another_layout_id)}"
            android:text="Click me" />
            
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

CodePudding user response:

Duplicate of Android Data Binding - Reference to view, I had to refer to the view id in camelCase as opposed to the snake case.

  • Related