WISHY
WISHY

Reputation: 11999

How to access view from included layout with kotlin synthetic in android

In my fragment layout I have added a layout with include

<?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>

  
</data>

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="3"
        tools:listitem="@layout/list_item_eceived" />

    <include
        layout="@layout/item_user_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

My included layout

<?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="hint"
        type="String" />
    <variable
        name="available"
        type="Boolean" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/message_box_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:elevation="2dp"
    android:minHeight="50dp"
    app:layout_goneMarginStart="6dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/message_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="4dp"
        android:focusable="true"
        android:hint="@{hint}"
        android:maxHeight="128dp"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/send_btn_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/send_btn_view"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_marginEnd="2dp"
        android:paddingStart="12dp"
        android:paddingTop="8dp"
        android:paddingEnd="8dp"
        android:paddingBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

I access the view in fragment

send_btn_view.setOnClickListener { 
        Log.e("button","clicked")
    }

The synthetic import is import kotlinx.android.synthetic.main.item_user_input.*

And when I run the application I get the error

Unresolved reference: item_user_input

Upvotes: 4

Views: 2778

Answers (1)

Shlomi Katriel
Shlomi Katriel

Reputation: 2403

You can’t, because Synthetic library won’t know what to do if the same layout will be included multiple times.

Anyway, Kotlin Synthetic is deprecated and Google officially recommends to migrate to view/data binding: https://developer.android.com/topic/libraries/view-binding/migration

Since you already use data binding I’d recommend using the binding class to access the view. This way you explicitly mention the included layout.

First, add an id to the included layout:

   <include
        android:id="@+id/item_user_input"
        layout="@layout/item_user_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

Second, access the view:

binding.itemUserInput.sendBtnView

Upvotes: 6

Related Questions