I created a compounded custom View using ConstraintLayout.
class CustomButtonView(
context: Context,
attrs: AttributeSet?
) : ConstraintLayout(context, attrs) {
constructor(context: Context) : this(context, null)
init {
val inflater = context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.custom_button_view, this, true)
super.setOnClickListener{
Timber.d("CLICKED")
}
this.setOnClickListener {
Timber.d("CLICKED")
}
rootView.setOnClickListener {
Timber.d("CLICKED")
}
}
}
The custom_button_view
xml is:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/icon_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/icon_camera" />
<TextView
style="@style/ButtonText"
android:id="@+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/text_drawable_margin"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/icon_camera"
android:text="@string/button_text" />
<ImageView
android:id="@+id/icon_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/text_drawable_margin"
android:layout_marginTop="@dimen/small_margin"
app:layout_constraintTop_toTopOf="@id/button_text"
app:layout_constraintBottom_toBottomOf="@id/button_text"
app:layout_constraintStart_toEndOf="@id/button_text"
android:src="@drawable/icon_dot" />
</merge>
And when I include it in a layout I just do:
<com.xxx.etc..CustomButtonView
android:id="@+id/custom_button"
style="@style/CustomButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I used to do this often in the past but I don't remember how I was making the click working. I have tried to set the click listener on the view as well:
binding.customButton.setOnClickListener {
Timber.d("CLICKED")
}
None of the clicks are working.