Data binding to androids <include> layout OnClick listener not working

I have a data binding layout that contains a frame layout with a different location inside:

<FrameLayout
    android:id="@+id/global_actions_frame_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.075"
    android:background="@color/colorToolBar">
    <include
        android:id="@+id/included"
        layout="@layout/global_actions">
    </include>
</FrameLayout>

      

The layout has image buttons inside this format:

<ImageButton
    android:id="@+id/settingsButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_settings_black_36dp"
    android:layout_gravity="end"
    android:background="@color/colorToolBar"
    android:layout_margin="4dp"
    android:layout_marginLeft="20dp"
    android:onClick="@{listener::onClickState}"
android:alpha="0.4"/>

      

and i added tag around them and i added data with activity name

 <data>
    <import type="android.view.View"/>
    <variable name="listener" type="MyActivity"/>
</data>

      

and in MyActivity, I called a function to listen on a click on the image buttons:

public void onClickState(View view){
int id = view.getId();
}

      

but for some reason i dont get this function when clicked i also tried

 android:onClick="@{listener.onClickState}"

      

but nothing helped .. Any ideas? thank!!

+3


source to share


2 answers


Activity:

activityMainBinding.included.setListener(this);

      



global_actions.xml ::

<layout>
    <data>
        <variable type="your.packages.here.MainActivity" name="listener"/>
    </data>

    <ImageButton
    android:id="@+id/settingsButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_settings_black_36dp"
    android:layout_gravity="end"
    android:background="@color/colorToolBar"
    android:layout_margin="4dp"
    android:layout_marginLeft="20dp"
    android:onClick="@{listener::onClickState}"
    android:alpha="0.4"/>
</layout>

      

+1


source


If you want to use variables DataBinding

in the included layout, you need to pass them to the included layout and also make your parent view DataBinding

:

<layout>
    <data>
        <variable type="your.packages.here.MainActivity" name="listener"/>
    </data>

    <FrameLayout>
        <include
            android:id="@+id/included"
            layout="@layout/global_actions"
            app:listener="@{listener}"/>
        </include>
    </FrameLayout>
</layout>

      

You need to set your listener in your MainActivity.class to the appropriate binding class:

activityMainBinding.setListener(this);

      



and in your included layout you need to use the same name you used in the parent layout ( app:listener

):

<layout>
    <data>
        <variable type="your.packages.here.MainActivity" name="listener"/>
    </data>

    <ImageButton 
     android:onClick="@{listener::onClickState}"/>
</layout>

      

Please see George Mounts' answer to a similar question.

+3


source







All Articles