13

I have a data binding layout that contains a frame layout with <include> of other layout 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 in 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 the tag around them and I added data with name of activity

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

and in MyActivity I called a function to listen to On click from the image buttons:

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

but from some reason I don't get to this function when I click I tried also

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

but nothing helped.

Cœur
  • 37,241
  • 25
  • 195
  • 267
batsheva
  • 2,175
  • 1
  • 20
  • 32
  • are you binding data in the included layout Variables may be passed into an included layout's binding from the containing layout by using the application namespace and the variable name in an attribute: – Shubham AgaRwal Apr 02 '17 at 17:29
  • https://developer.android.com/topic/libraries/data-binding/index.html – Shubham AgaRwal Apr 02 '17 at 17:30
  • I'm using binding with include with for first time, it is working fine, I just include the layout and use ```binding.included_layout.textview.setOnClickListener(....)``` – Azhar Ali Apr 04 '21 at 11:31

2 Answers2

15

If you want to use DataBinding variables in an included layout, you need to pass them to the included layout, and also make your parent view use 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 corresponding binding class:

activityMainBinding.setListener(this);

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

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

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

Please take a look at George Mounts answer to a similiar question.

Community
  • 1
  • 1
yennsarah
  • 5,467
  • 2
  • 27
  • 48
  • hi, i don't think that you have to set the listener in the parent too, it worked to me without it, only in the include layout.. in the main activity i added: mBinding.included.setListener(this); – batsheva Apr 04 '17 at 05:02
  • 1
    Yes, this also works. But in that case you needed to assign an id to your `` – yennsarah Apr 04 '17 at 05:18
8

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>
S Haque
  • 6,881
  • 5
  • 29
  • 35