Yevhenii
Yevhenii

Reputation: 840

How to pass OnClickListener to included layout?

I need to pass OnClickListener from parent to included layout.

I have a layout with an included view that accepts OnClickListener:

<?xml version="1.0" encoding="utf-8"?>
<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="clickListener"
        type="android.view.View.OnClickListener" />

    </data>

         <include // I need to pass here
            android:id="@+id/button_more"
            layout="@layout/button_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

And the included layout (button_more.xml):

<?xml version="1.0" encoding="utf-8"?>

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

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

<ImageButton
    android:id="@+id/button_more"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?selectableItemBackgroundBorderless"
    android:onClick="@{listener}"
    android:padding="12dp"
    android:src="@drawable/ic_more_vert_black_24dp"
    app:tintColor="@{android.R.attr.textColorPrimary}" />

How to pass OnClickListener to included layout?

Upvotes: 2

Views: 283

Answers (1)

Artur Gniewowski
Artur Gniewowski

Reputation: 500

U can pass clickListener from parent to included layout in this way:

<include layout="@layout/button_more"
         app:listener="@{clickListener}"/>

Upvotes: 2

Related Questions